Sorted Map Collection Tutorial for Java Beginners

 Salient Point of SortedMap

  • This interface is the map analogue 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.
  • In 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 concrete class
    • No Arg Constructor
    • Constructor with single argument of Comparator.
    • Constructor with single argument as Map.
    • Constructor with 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.

 

Sorted Map by Key

Sorted Map by Value

 

Sorted Map Example

Sorted Map Comparator Example

Sorted Map Duplicate

Leave A Comment

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