aspose file tools*
The moose likes Java in General and the fly likes How to sort Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to sort Map" Watch "How to sort Map" New topic
Author

How to sort Map

Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


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.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Apr 15, 2008
Posts: 131
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();

<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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
 
subject: How to sort Map
 
Similar Threads
how to pass parameters
moved class files into new package, application stopped working...
opening database in servlet
ArrayList vs LinkedHashMap
How to get rid of NullException Pointer?