• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorted map but not sorted keys - How?  RSS feed

 
Jonny Andersson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to store entrys with names that are not unique in a map. Because the names are not unique do I have an int as the key and I want to be able to access a specific entry using the int like mayMap.get(6). So far no problem.

But, when I traverse the map using an iterator to for example print the names do I want them in order of their name! Not in the order of their key! And the SortedMap TreeMap sorts the entries in order of their keys, not what I want! I want it to sort Comparable entries in order of the return from the compareTo method. I want to implement the compareTo method in the entry class so that it accept another entry to let their names, not the keys, decide the order. But this is not what the TreeMap does, that map compare the keys, nothing else. Surely good at many times, but not what I want.

So far is my idea to make my entries Comaparable and build my own little binary sort (quicksort) ... No problem but I always prefer use library classes if possible. Any idea on how I can avoid create my own sorting routine? This should already be in a complete library, shouldn�t it? Maybe have I just not found it yet : )

[ July 01, 2006: Message edited by: Jonny Andersson ]

[ July 01, 2006: Message edited by: Jonny Andersson ]
[ July 01, 2006: Message edited by: Jonny Andersson ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should be able to do something along the lines of

sortedNames = new SortedSet(myMap.valueSet());

Does that help?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far is my idea to make my entries Comaparable and build my own little binary sort (quicksort) ... No problem but I always prefer use library classes if possible. Any idea on how I can avoid create my own sorting routine? This should already be in a complete library, shouldn�t it? Maybe have I just not found it yet : )


Another option, if it is okay to sort only once... place all of your items into a List instead. When you are done, use the collection's toArray() method to get an array. And then use the Arrays.sort() method to sort the array.

Henry
 
Jonny Andersson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of thanks for your ideas! It is very interesting and instructive to read ideas from other people.

At first did I prefer Henry�s idea because a Set only can contain unique objects and I want to store entries with equal name. But then I realized that the uniquness by default is reference to the same object so that would not be a problem. And even if I implemented an equals-method for the entries would it be okay because there will at least never be two entries with equal id.

So now do I instead prefer Ilja�s solution because it feels a bit more object oriented with a collection object where I still can request an iterator.

I would prefer a solution where I could have a map where the entries always is stored in a sorted order on their name and accessible by their id, but without having to do a re-sort every time a new entry is added. But it is not that necessary.

So using Ilja�s solution will I now instead have a getter that returns a Collection (because it gives most flexibility to change of the implementation between a List or a Set in future) which for now is implemented as a TreeSet which is created from the original map every time the getter is called. performance is not an issue so that is okay.



Both of your solutions required far fewer lines of code that my original idea would have required : )
[ July 01, 2006: Message edited by: Jonny Andersson ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!