Exploring Sorted Maps

TreeMap is an example of a sorted map, which is a map that maintains its entries in ascending order, sorted according to the keys’ natural ordering or according to a comparator that is supplied when the sorted map is created. Sorted maps are described by the SortedMap interface.

SortedMap (whose generic type is SortedMap<K, V>) extends Map. With two exceptions, the methods it inherits from Map behave identically on sorted maps as on other maps:

  • The Iterator instance returned by the iterator() method on any of the sorted map’s Collection views traverses the collections in order.
  • The arrays returned by the Collection views’ toArray() methods contain the keys, values, or entries in order.

Note  Although not guaranteed, ...

Get Learn Java for Android Development, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.