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