JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Java-Collections Java-Map-Programs java-TreeMap Picked Java treemap. Show all posts
Showing posts with label Java-Collections Java-Map-Programs java-TreeMap Picked Java treemap. Show all posts

How to write a program to create a TreeMap in reverse order in Java

9:15 PM
  How to write a program to create a TreeMap in reverse order in Java By default TreeMap elements in Java are sorted in ascending order of keys. However, we can create the TreeMap in reverse order using Collections.reverseOrder() method in Java and display the elements in descending order of keys. The Collections.reverseOrderS() method in Java returns a Comparator that imposes reverse order of a passed Comparator object. We can use this method to sort any list or any other collection in reverse order of user defined Comparator. Examples: // Insert elements to the TreeMap Input : treemap.put("1", "Welcome");              treemap.put("2", "to");              treemap.put("3", "the");              treemap.put("4", "that");              treemap.put("5", "Community"); // Elements should be printed in reverse order // of their insertion Output : 5: Community                4: that              

.