• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Sorting Problem

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic