Understanding Collections Framework map structures
The second family of collections is the map family. A map is a data structure in which you add elements to a map with a pair of values. The first value is the key. This is a reference to an object that, depending on the type of map, either implements the Comparable
interface—as we saw in the previous section—or overrides the hashCode
and equals
methods. If the key is a primitive, then we declare it as its wrapper class, and Java will manage the necessary conversion to and from the primitive. The second is the value—a reference to the object you are storing in the map. This class does not need to implement the Comparable
interface.
There are three map implementations in Java, which we will now cover.
HashMap
Of all the data types available in Java and most other languages, the fastest performing is the integer. The size of an integer is the same as the word size of the CPU. The JVM is a 32-bit or 4-word...