Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sort a Map on the <Key>.

 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

How do we sort a <Map> on the key? I am using a java.util.HashMap.

Thanks,
Raj.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can use TreeMap
 
vikram Kesav
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dump the HashMap into a TreeMap:

Map hashMap= new HashMap();
Map sortedMap = new TreeMap(hashMap);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic