Dave's Brain

Browse - Programming Tips - How can I convert a HashMap's values into a sorted ArrayList?

Date: 2016mar9
Language: Java
Keywords: to

Q.  How can I convert a HashMap's values into a sorted ArrayList?

A.	ArrayList's constructor takes a HashSet so its pretty easy:

HashMap<String,Integer> h = someCodeToMakeTheHash(); ArrayList<Integer> vals = new ArrayList<Integer>(h.values()); Collections.sort(vals);
h.values() gives as HashSet.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.