Monday, January 12, 2026

 TreeMap Sorting Example

-----------------------------------

import java.util.TreeMap;

public class TreeMapExample {

public static void main(String[] args) {

TreeMap<Double, String> treeMap = new TreeMap<Double, String>((I1,I2)->(I1>I2)?1:(I1<I2?-1:0));

treeMap.put(200.0, "initial");

treeMap.put(300.0, "Final");

treeMap.put(100.0, "Rather");

treeMap.put(1000.0,"No Worries");

System.out.println("treeMap Values in ascending Order is " + treeMap);

}

}


Solution :


treeMap Values in ascending Order is {100.0=Rather, 200.0=initial, 300.0=Final, 1000.0=No Worries}


treeMap Sorts according to its Key and therefore the result as 100,200,300,1000.....

No comments:

Post a Comment