Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Problem

 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a certain programming problem, I have 2 things:
1)A methods name.
2)It's parameters as Class objects arrays.
I have a HashTable containing this 2 with the keys as the Parameters array object, and the corresponding value as the corresponding method's name. I want to sort this set of data based on the method names. Please help me on how to do this. Is it possible to do through some collection class or it can be done through pure programming logic. The logic which I have currently applied is as follows :
I take the Collection of the values in the HashTable, convert it in an array, then sort it. Then I take each element from the array currently formed, iterate through the HashMap, find the corresponding key, and then put this key-value pair in a new HashTable. Thus the new HashTable contains the sorted values. I know this solution is a terrific one on part of efficiency, but I don't have any other solution. Please help me out
to have a better solution.
Many,many thanks in advance
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you'll probably want to do is extract the entrySet() from your Map, then sort them with a custom ComparatorYou can sort by turning the Set into a List and sorting that - Peter
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit mystified though by your reference to the final HashTable (you mean HashMap, I hope) containing sorted values. Hash-anything doesn't preserve sorting order.
- Peter
 
Anirban dutta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear peter,
Yah you pointed it out right, I referenced everywhere HashMap as HashTable. I am very sorry for that. I find the solution given by you to be an elegant one. Will you please elborate on how to use a custom Comparator.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your can learn more about Comparator through the Java Tutorial, or most good basic Java texts. In Peter's code, the entrySet() method returns a Set of Map.Entry objects. Each of these objects cointains a key and a value; you can use getKey() and getValue() to access them. Peter is assuming you really do need to sort things by value (rather than key), so his Comparator uses getValue to access the values of two different Map.Entry objects and compare them.
I agree with Peter that the idea of ordering a HashMap is... odd. Hashtable and HashMap have no order really (none worth trying to understand anyway). If you want a Map which is sorted based on the order of the keys, you could use a TreeMap. If you really want something sorted by value, I suppose you could use a LinkedHashMap. After sorting the ArrayList containing all the entries, put them in a LinkedHashMap:

Now any time you iterate through the entrySet(), keySet(), or values() of this map, elements will be found in the order they were inserted. If you add any new elements to the map later, they will occur at the end - probably you would need to re-sort all the entries again and put them in a new LinkedHashMap. (Or remove them all from the old map and then re-insert them in the new order.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic