File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to change order of putting <keys,values> in hashMap

 
srikanth Thandra
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I am getting some column names from database like Request_id,Name,Address,Request_status.
Now i am putting the above values as key in a hash Map. When i display the hash Map the order it is showing as Name,Address,Request_status,Request_id.

My requirement is i want the Request_id at first in hash map when displaying. Is there any way to do that.

I have used Tree Map but it gives sorted map and displays Address,Name,Request_id,Request_status. This is not my requirement since it displays Request_id at 3rd place. I want Request_id at 1st place.


Please give me suggestions on achieving this. Waiting for the reply.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A HashMap does not have an order. You can view a HashMap as a bag that contains a number of items - the items are jumbled up in the bag, and not in any particular order.

You can use LinkedHashMap instead of HashMap, which keeps the entries in the order that you inserted them.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue that if you want a certain value at a certain place, you don't want to use a map at all.

If you always want Name to be first, what's wrong with an ArrayList?
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or create a class to hold the values and override the toString method to print them in the order you want.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic