Christoph H. :
I have a HashMap<String, Float>
that gets filled with several entries. I want to save the keys of the HashMap into an ArrayList<String>
, but sort this ArrayList according to the corresponding values of the HashMap.
Is there a better solution than sorting the HashMap, then using
ArrayList<String> sortedKeys = new ArrayList<String>(myHashmap.keySet());
By the way I'm not bound to using a HashMap in the first place, but the value sets the order and I don't want swap key and value since the float may change several times.
Sweeper :
If I understand correctly, you can create the array list from the keys first, and then sort:
ArrayList<String> list = new ArrayList<>(myHashMap.keySet());
list.sort(Comparator.comparing(myHashMap::get));