1. Maps
Our final Java Collection type to explore is `Map`.
2. Maps are lookup tables
`Map`s store objects in key/value pairs. A key object points to a value object in a lookup table. As an analogy, a doctor looks up a health record as a value using a name and date of birth identification as a key.
3. Map interface
The `Map` interface defines how to store or put, remove, and retrieve value objects based on a key object. As you might expect, there are several implementations of `Map` interface. We will explore `HashMap`.
4. HashMap keys and values
HashMap key and value objects do not have to be the same type. Here they are different as the keys are `Short` objects and the values are `String` objects. Keys cannot be null and we can't have duplicate keys. The value object that keys point to can be null and different key objects can point to duplicate value objects.
5. HashMap construction
As with `List`, `Set` and `Queue` types, to construct a `HashMap` use `new` with the constructor. However, a `Map` like `HashMap` requires we specify two generics parameters: one for key type and the other for value type. In this example, the keys will be `Integer` objects pointing to `String` value objects.
The key and value types can be different or the same.
`HashMap` is in the `java.util` package and requires its import when used.
6. HashMap methods
We use `.put(key, value)` to add new key/value pairs to the stored data in the `HashMap` and `.remove(key)` to delete the key/value pair via the key object. The `.get(key)` method returns the value object at the specified key object. Note, null value and duplicate value objects are allowed in `Map` objects. But the key cannot be null or a duplicate object. From the printout of the `Map`, it may look like key/value pairs are ordered by the order they were added. However, Java says this order is not guaranteed.
7. Collections
Applications usually utilize many Collections Framework data structures. We are often having to move data from one to another data structure. The `java.util` package contains a supporting class, called `Collections` that provides help in this endeavor. It provides a number of sort, fill, copy, search and other static methods to help when working with any Java Collections framework data structure. Recall static methods are those that belong to the class rather than a specific instance.
8. Collection methods
As shown in these examples, we can use `Collections` to add multiple objects to a list, count the number of times an object is in a `Collection`,
reverse the order of a list, sort the order of a list, or replace all the objects in a list with a specific object.
9. Arrays
There are times when we need to convert a Java array to a `List`. Recall that arrays are not resizable. We can convert it to a `List` and then grow or shrink the `List`. `List` also comes with more functionality thus providing more capabilities than arrays.
The `java.util` package contains a supporting class,`Arrays` that provides a static method to convert a Java array to a `List`.
10. Arrays example
Here's an example use of Arrays and its `asList()` method to turn an array into a `List`.
11. Let's practice!
Time to wrap up our exploration of the Java Collections framework by trying out some `Map` objects.