This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes ArrayList/HashMap - Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList/HashMap - Sorting" Watch "ArrayList/HashMap - Sorting" New topic
Author

ArrayList/HashMap - Sorting

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
List<HashMap<String, Integer>> I want to store sorted elements in List. Can i use comparator for Hashmap sorting or any other way ?


[HENRY: fixed brackets corruption]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Can i use comparator for Hashmap sorting or any other way ?


Not exactly sure what you are asking? Can you elaborate? Possible answers...

1. There is no concept of a sorted list, like TreeMap but for lists, in the core Java API, that I know of. But I believe that there are a bunch of third party implementations available.

2. You can sort a List with the collections class. And yes, it takes a comparator.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
Henry Wong, Basically i would like to sort HashMap based on the Integer value HashMap<String, Integer>> .
can you give the link for third party implementation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Write down the contents of a typical Map which you wish to sort.
Write down how you consider one Map to be "greater" or "less" than another Map.
Then you can implement a Comparator which will enable such sorting.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
I don't think that will work. A Comparator is used to sort the keys of a SortedMap. It's of no use sorting a value, which seems to be what kri shan wants. Unfortunately, trying to sort a Map this way is pretty much doomed to failure.

Kri, if you want to sort by the Integer values, you're really going to need a second collection, independent of the Map. Like a List<Integer>, or maybe a List<Map.Entry><String, Integer>>. Or a SortedSet<Integer> or SortedSet<Map.Entry><String, Integer>>. Whenever you put something in the Map, you also put it in the List or Set. (You'll have to re-sort the List; a SortedSet will stay sorted.)

Alternately, maybe you could tell us why you want a Map sorted this way. There may well be a different way to approach the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList/HashMap - Sorting
 
Similar Threads
Struggling with generics, "unchecked call to put()"
generics and maps
is object a list of string or only a string?
How to sort Hashmap in case your Hashmap contains real life objects
HashMap, DropDown List, order of values