Title: Map - SortedMap Interfaces
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER > Section: Learning Units - Bb

Presentations and Videos

- Map - SortedMap Interfaces lecture video (Time: 52:00)
- Map - SortedMap Interfaces document

- The Map interface is used for data structures where we need fast lookup of data objects based on some Key. The Map interface does not extend the Collection interface and it has the following characteristics:Dogs.java example: A Dogs class is discussed to illustrate a class based on the Map interface and it demonstrates the methods: put, containsValue, containsKey, get, keySet, values, and entrySet.
  - Each entry consists of a Value and a Key.
  - The method put(Key, Value) is used for adding a new entry to the Map interface. Keys may not be duplicated, but the same Value entry can inserted more than once with different Keys.
  - The get(Key) method returns the entry associated with the parameter Key.
  - The Map interface has a method called containsKey to find out if a particular Key exists. The method keySet returns a "Set" of all of the Keys. The returned Set can then be iterated through using the concepts we have already covered with the "Set" interface.
  - The Map interface has a method called containsValue to find out if a particular Value can be found in the Map. If you call the "values" method, you will get a "Collection" of all of the Values in the Map. This "Collection" can then be iterated through using the concepts we have already covered with the "Collection" interface.
  - The HashMap, and LinkedHashMap classes implement the Map interface.
- Statistics.java example: A Statistics class is presented to also demonstrate using the Map interface.
- StudentRecords.java example: A more complicated example is presented where the Key is more complicated than a simple String or integer. In this example, the Key is a Student class. If we are using HashMap or LinkedHashMap classes, then the Student class will need to implement the methods "equals" and "hashCode" to properly work in the required hashing approach. Note that there are many ways to create a hashCode method, but in this example we see at least one approach for our Student class.
- The SortedMap interface extends the Map interface. A SortedMap interface implies that the Keys are stored in a sorted fashion. Similar to the SortedSet interface, the SortedMap interface has a few extra methods available because of the sorted property: firstKey, headMap, lastKey, subMap, tailMap.
- The one class provided that implements the SortedMap interface is the TreeMap class. Like the TreeSet class, we find that the keys stored in a TreeMap must implement the Comparable interface. This is necessary to maintain a binary tree of keys which allows us to easily provide a sorted list of keys.
- All of our examples can be updated to work with TreeMap. For example in the Dogs.java example the main change needed here is that Dog must implement the Comparable interface. Also instead of constructing HashMap, we construct TreeMap.