• 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

Distinct list ov values from a Map

 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any method to get the unique (in sorted order) from a Map? In my case it's a HashMap (for now).
Thanks,
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just a case of using the right methods - the API is always a good place to start.
In this case I'd use keySet() or valueSet() from the Map class, then convert to an array and sort (using methods from Set and Arrays respectively).
Elements of a Set are by definition distinct - assuming you define 'distinct' based on the equals() method.
-Tim
[ April 26, 2004: Message edited by: Tim West ]
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the entries to be sorted by their keys, then a TreeMap is a standard solution. All you need to do is to add the items to the Map, and it will resort it for you. If that's what you want, all you may need to do is to replace the reference to HashMap to TreeMap in your code -- both implement the Map interface. If you need to sort by values, there is a little more work that may involve writing your own Comparator class and passing it to the sort method.
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case I'd use keySet() or valueSet() from the Map class
I did not find valueSet() in the API
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try values(). It's not a Set, since values can be duplicated. To get a sorted list of distinct values, you can use
new TreeSet(map.values())
 
reply
    Bookmark Topic Watch Topic
  • New Topic