• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to sort Map

 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • 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.


 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • 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.
 
Rancher
Posts: 43016
76
  • 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.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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
Posts: 43016
76
  • 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.
 
Rancher
Posts: 3625
40
  • 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.
 
Ranch Hand
Posts: 131
  • 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
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic