java treemap comparator 28 29 method example

Solutions on MaxInterview for java treemap comparator 28 29 method example by the best coders in the world

showing results for - "java treemap comparator 28 29 method example"
Louis
23 Oct 2020
1// Java treemap comparator() method example
2import java.util.Comparator;
3import java.util.NavigableMap;
4import java.util.TreeMap;
5public class TreemapComparatorExample 
6{
7   public static void main(String[] args) 
8   {
9      NavigableMap<Integer, String> nm = new TreeMap<Integer, String>();
10      // populating tree map
11      nm.put(101, "apple"); 
12      nm.put(102, "banana"); 
13      nm.put(103, "apricot"); 
14      nm.put(104, "blackberry"); 
15      nm.put(105, "avocado"); 
16      // printing TreeMap 
17      System.out.println("TreeMap: " + nm);
18      // using comparator() method
19      Comparator c = nm.comparator();
20      // print comparator value
21      System.out.println("Comparator value: " + c);
22   }
23}