Open In App

SortedMap comparator() method in Java with Examples

Last Updated : 08 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The comparator() method of java.util.SortedMap interface is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
Syntax: 
 

public Comparator comparator()


Return Value: This method returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
Below programs illustrate the comparator() method:
Example 1: For Natural ordering. 
 


Output: 
SortedTreeMap: {1=one, 2=two, 3=three, 4=four, 5=five}
Comparator value: null

 

Example 2: For Reverse ordering. 
 


Output: 
SortedTreeMap: {5=five, 4=four, 3=three, 2=two, 1=one}
Comparator value: java.util.Collections$ReverseComparator@232204a1

 

Next Article

Similar Reads