-2

For example you have a HashMap<String, String>

how can you sort values in the hashmap and print them to console

what would be the best ways to do it?

VextoR
  • 5,087
  • 22
  • 74
  • 109
  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see [ask] – gnat Jan 23 '14 at 14:05
  • 1
    Why do you want / use a HashMap rather than, say a TreeMap? –  Jan 23 '14 at 14:18
  • @MichaelT a TreeMap would sort on the keys – ratchet freak Jan 23 '14 at 14:26
  • @ratchetfreak ahh, that's what I get for commenting while still waking up in the morning. –  Jan 23 '14 at 15:06
  • @VextoR This is part of the same network as Stack Overflow and does not share the same scope. –  Jan 23 '14 at 15:07
  • 1
    http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java here shows how to use a value comparator in a TreeMap – Lorenzo Boccaccia Jan 23 '14 at 15:12
  • @VextoR Programmers.SE is not the place for implementation questions, which this is. Furthermore, it is *always* helpful to people answering the question to know what you are working from, the problem domain and similar. Just because you didn't have code with it doesn't mean that its a P.SE question (and neither SO, nor P.SE are forums). Reading the [help/on-topic] on each of the sites is quite useful for getting an idea of where a particular question belongs. –  Jan 23 '14 at 16:35

1 Answers1

4

This should do it:

Map<String, String> asso = new HashMap<String, String>
// add some tuples to asso
List<String> values = new ArrayList(asso.values());
Collections.sort(values); // assumes an appropriate comparator implementation for the value type

There isn't a way to retrieve a HashMap tuple by value sorted order afaik.

Bhaskar
  • 594
  • 2
  • 11