20.11 Maps
Maps associate keys to values. The keys in a Map
must be unique, but the associated values need not be. If a Map
contains both unique keys and unique values, it’s said to implement a one-to-one mapping. If only the keys are unique, the Map
is said to implement a many-to-one mapping —many keys can map to one value.
Map
s differ from Set
sinthat Map
s contain keys and values, whereas Set
s contain only values. Three of the several classes that implement interface Map
are Hashtable, HashMap and TreeMap. Hashtable
sand HashMap
s store elements in hash tables, and TreeMap
s store elements in trees. This section discusses hash tables and provides an example that uses a HashMap
to store key/value pairs. Interface SortedMap extends Map
and maintains ...
Get Java How to Program (early objects), 9/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.