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 sort Map

 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello every one,


I am using one HasMap which contains Integer key and List type value. Like.

private Map<Integer ,Items> itemMap = new HashMap<Integer, Items>();

items is POJO DTO hibernate calass Objects.

So how can i sort using values contains item name.

Items contains item Title Name.

So, I want to know how to sort using item name.


 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure whether i understood what is the content of your list but if you want to sort the Items then you have to get the values using values() and then sort it as you would sort any other list. (Colections.sort())

Moving to Beginners forum.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is important to realize that a Map has no sort order. You can either sort the list of keys or sort the list of values (as Nitesh said). Neither is going to impact the Map in any way.
 
Rob Spoor
Sheriff
Pie
Posts: 20384
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:It is important to realize that a Map has no sort order.

The SortedMap interface, and its implementation TreeMap, disagree with you

However, these only impose an order on the keys, not on the values. There is no known implementation that sorts on the values, and the reason is clear: it basically turns your Map into a list of Map.Entry objects as far as performance goes. get() looks up values based on a key, and if the Map is sorted on the value the Map will have to check every entry until a match is found.

Of course, if you really need this, you can implement it yourself:
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:The SortedMap interface, and its implementation TreeMap, disagree with you

Not really - a Map is not a SortedMap.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But would you agree that a SortedMap is a Map? And thus, some Maps do have sort order, even while others do not?

I think when you say "a Map has no sort order", it sounds to Rob and me as if you're saying no Map has sort order, when you probably mean that a Map, in general, cannot be assumed to have sort order. Unless of course one has more specific knowledge about the type of Map they're dealing with.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nishan Patel wrote:
Hello every one,


I am using one HasMap which contains Integer key and List type value. Like.

private Map<Integer ,Items> itemMap = new HashMap<Integer, Items>();

items is POJO DTO hibernate calass Objects.

So how can i sort using values contains item name.

Items contains item Title Name.

So, I want to know how to sort using item name.




If you want to sort the Map using the keys then use Treemap so
SortedMap map = new TreeMap();
 
Rob Spoor
Sheriff
Pie
Posts: 20384
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshit Rastogi wrote:If you want to sort the Map using the keys then use Treemap so
SortedMap map = new TreeMap();

Correct, but Nishan does not want to sort on the keys but on a part of the values.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic