Java Reference
In-Depth Information
The
HashMap
class is efficient for locating a value, inserting an entry, and deleting an entry.
LinkedHashMap
extends
HashMap
with a linked-list implementation that supports an
ordering of the entries in the map. The entries in a
HashMap
are not ordered, but the entries
in a
LinkedHashMap
can be retrieved either in the order in which they were inserted into the
map (known as the
insertion order
) or in the order in which they were last accessed, from
least recently to most recently accessed (
access order
). The no-arg constructor constructs a
LinkedHashMap
with the insertion order. To construct a
LinkedHashMap
with the access
order, use
LinkedHashMap(initialCapacity, loadFactor, true)
.
The
TreeMap
class is efficient for traversing the keys in a sorted order. The keys can
be sorted using the
Comparable
interface or the
Comparator
interface. If you create a
TreeMap
using its no-arg constructor, the
compareTo
method in the
Comparable
interface
is used to compare the keys in the map, assuming that the class for the keys implements the
Comparable
interface. To use a comparator, you have to use the
TreeMap(Comparator
comparator)
constructor to create a sorted map that uses the
compare
method in the com-
parator to order the entries in the map based on the keys.
SortedMap
is a subinterface of
Map
, which guarantees that the entries in the map are
sorted. Additionally, it provides the methods
firstKey()
and
lastKey()
for returning the
first and last keys in the map, and
headMap(toKey)
and
tailMap(fromKey)
for returning
a portion of the map whose keys are less than
toKey
and greater than or equal to
fromKey
,
respectively.
NavigableMap
extends
SortedMap
to provide the navigation methods
lowerKey(key)
,
floorKey(key)
,
ceilingKey(key)
, and
higherKey(key)
that return keys respectively
less than, less than or equal, greater than or equal, and greater than a given key and return
null
if there is no such key. The
pollFirstEntry()
and
pollLastEntry()
methods
remove and return the first and last entry in the tree map, respectively.
HashMap
LinkedHashMap
insertion order
access order
TreeMap
SortedMap
NavigableMap
Note
Prior to Java 2,
java.util.Hashtable
was used for mapping keys with values.
Hashtable
was redesigned to fit into the Java Collections Framework with all its methods
retained for compatibility.
Hashtable
implements the
Map
interface and is used in the
same way as
HashMap
, except that the update methods in
Hashtable
are synchronized.
Hashtable
Listing 21.8 gives an example that creates a
hash map
, a
linked hash map
, and a
tree map
for
mapping students to ages. The program first creates a hash map with the student's name as
its key and the age as its value. The program then creates a tree map from the hash map and
displays the entries in ascending order of the keys. Finally, the program creates a linked hash
map, adds the same entries to the map, and displays the entries.
L
ISTING
21.8
hash map
linked hash map
tree map
TestMap.java
1
import
java.util.*;
2
3
public class
TestMap {
4
public static void
main(String[] args) {
5
// Create a HashMap
6 Map<String, Integer> hashMap =
new
HashMap<>();
7 hashMap.put(
"Smith"
,
30
);
8 hashMap.put(
"Anderson"
,
31
);
9 hashMap.put(
"Lewis"
,
29
);
10 hashMap.put(
"Cook"
,
29
);
11
12 System.out.println(
"Display entries in HashMap"
);
13 System.out.println(hashMap +
"\n"
);
14
15
create map
add entry
// Create a TreeMap from the preceding HashMap
16
Map<String, Integer> treeMap =
tree map
Search WWH ::
Custom Search