Sorted Map Collection Tutorial for Java Beginners

 Salient Point of SortedMap

  • This interface is the map analog of SortedSet.
  • IT provides ordering on its keys.
  • Sorted criteria would be to natural order or Comparator provided at the time of creation.
  • Keys used in it must implement Comparable or accept the specified comparator.
  • Keys in this map need to be mutually comparable.
  • Map interface equals method is used to compare two keys while in SortedMap compare/ compareTo method (from Comparable or Comparator) is used.
  • Constructor that needs to be implemented by the concrete class
    • No-Arg Constructor
    • Constructor with a single argument of Comparator.
    • Constructor with a single argument as Map.
    • Constructor with a single argument as SortedMap.
  • SortedMap is the part of Java Collection Framework.

Important Method in SortedMap Interface

  • subMap(K fromKey, K toKey): Returns a view of the portion of this map whose elements range from fromElement, inclusive, to toElement, exclusive.
  • headMap(K toKey): Returns a view of the portion of this map whose elements are strictly less than toElement.
  • tailMap(K fromKey): Returns a view of the portion of this map whose elements are greater than or equal to fromElement.

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.