• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList/HashMap - Sorting

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic