• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 23928
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: 76881
366
  • 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.
 
Rancher
Posts: 4331
59
  • 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.
 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic