Class AbstractSortedMap<K,V>

java.lang.Object
com.zybooks.dsaj.map.AbstractMap<K,V>
com.zybooks.dsaj.map.AbstractSortedMap<K,V>
Type Parameters:
K - The key type (keys must be unique and comparable)
V - The value type
All Implemented Interfaces:
Map<K,V>, SortedMap<K,V>
Direct Known Subclasses:
SortedTableMap, TreeMap

public abstract class AbstractSortedMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>
An abstract base class to ease the implementation of the SortedMap interface. The base class provides the following means of support: 1) It defines a PQEntry class as a concrete implementation of the entry interface 2) It provides an instance variable for a general Comparator and protected methods, compare(a, b), that can perform key-key, entry-entry, or key-entry comparisons using the comparator.
  • Constructor Details

    • AbstractSortedMap

      protected AbstractSortedMap(Comparator<K> c)
      Initializes the comparator for the map.
      Parameters:
      c - comparator defining the order of keys in the map
    • AbstractSortedMap

      protected AbstractSortedMap()
      Initializes the map with a default comparator.
  • Method Details

    • compare

      protected int compare(Entry<K,V> a, Entry<K,V> b)
      Compares two entries according to their keys.
      Parameters:
      a - the first entry to be compared
      b - the second entry to be compared
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
    • compare

      protected int compare(K a, Entry<K,V> b)
      Compares a raw key and an entry's key.
      Parameters:
      a - the key to be compared
      b - the entry whose key should be compared
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the key of the second argument.
    • compare

      protected int compare(Entry<K,V> a, K b)
      Compares an entry's key and a raw key
      Parameters:
      a - the entry whose key should be compared
      b - the key to be compared
      Returns:
      a negative integer, zero, or a positive integer as the first argument's key is less than, equal to, or greater than the second argument.
    • compare

      protected int compare(K a, K b)
      Compares two raw keys
      Parameters:
      a - the first key to be compared
      b - the second key to be compared
      Returns:
      a negative integer, zero, or a positive integer as the first key is less than, equal to, or greater than the second key.