TreeMap will automatically sort in ascending order. If you want to sort in descending order, use the following code:
Copy the code below into your class and outside the main execute method:
static class DescOrder implements Comparator<String> { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }
Then by your logic
TreeMap<String, String> map = new TreeMap<String, String>(new DescOrder()); map.put("A", "test1"); map.put("C", "test3"); map.put("E", "test5"); map.put("B", "test2"); map.put("D", "test4");
JavaGeek Dec 20 '18 at 12:36 2018-12-20 12:36
source share