вы можете построить TreeSet
, который гарантирует порядок вставки:
@Test
public void treeMapSortedByValue() {
// given the following map:
TreeMap<Integer, Double> map = new TreeMap<Integer, Double>();
map.put(2, Math.E);
map.put(1, Math.PI);
map.put(3, 42.0);
// build a TreeSet of entries
Set<Map.Entry<Integer, Double>> sortedEntries = new TreeSet<Map.Entry<Integer, Double>>(new DoubleComparator());
sortedEntries.addAll(map.entrySet());
// optionally you can build a List<Double> with the sorted
List<Double> doubles = new LinkedList<Double>();
for (Map.Entry<Integer, Double> entry : sortedEntries) {
doubles.add(entry.getValue());
}
}
это должно дать вам: [2.718281828459045, 3.141592653589793, 42.0]
(nb: [Math.E, Math.PI, Math.UNIVERSAL_ANSWER]
:-).
PS
the Comparator
:
class DoubleComparator implements Comparator<Map.Entry<Integer, Double>> {
@Override
public int compare(Entry<Integer, Double> o1, Entry<Integer, Double> o2) {
return Double.compare(o1.getValue(), o2.getValue());
}
}