Chapter 15. Maps
The Map interface is the last of the major Collections Framework interfaces, and the only one that does not inherit from Collection. A Map stores key-to-value associations, or entries, in which the keys are unique. The importance of this interface lies in the fact that its implementations provide very fast—ideally, constant-time—operations to look up the value corresponding to a given key.
The Map interface overrides the equals and hashCode methods and provides definitions for them (hashCode should always be overridden when equals is overridden, as we saw in the discussion of hash tables in “Implementations”). For the purposes of these methods, a Map is considered as a Set of map entries (key-value pairs). As with the Set contract, a Map can only ever be equal to another Map, and then only if they are the same size and contain equal entries. The hash code of a Map is the sum of the hash codes of its entries. A map entry is defined by the Map.Entry interface. Two map entries are equal if both their keys and values are equal, and the hash code of a map entry is defined to be the result of applying an exclusive OR operation on the hash codes of the key and the value.
Note
The code examples for this chapter can be found at:
https://github.com/MauriceNaftalin/JGC_2e_Book_Code/blob/main/src/main/java/org/jgcbook/chapter15
Map Interface Methods
The methods of Map can be divided according to the two ways in which a map can be seen: as a set of entries or as a lookup ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access