[英]How to sort a HashMap by value but keep the order of duplicates?
I'm trying to sort the tableProbability
map into a new one called sorted
.我正在尝试将tableProbability
map 排序为一个名为sorted
的新表。 In tableProbability
the values are the following:在tableProbability
中,值如下:
Key钥匙 | Value价值 |
---|---|
M米 | 0.1 0.1 |
U ü | 0.3 0.3 |
L大号 | 0.3 0.3 |
T吨 | 0.2 0.2 |
I我 | 0.1 0.1 |
I have the following code that sorts the Map
:我有以下代码对Map
进行排序:
LinkedHashMap<Character, Double> sorted = new LinkedHashMap<>();
tableProbability.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.forEachOrdered(x -> sorted.put(x.getKey(), x.getValue()));
But what I end up getting is the following Map
:但我最终得到的是以下Map
:
Key钥匙 | Value价值 |
---|---|
L大号 | 0.3 0.3 |
U ü | 0.3 0.3 |
T吨 | 0.2 0.2 |
I我 | 0.1 0.1 |
M米 | 0.1 0.1 |
And what I am supposed to get is:我应该得到的是:
Key钥匙 | Value价值 |
---|---|
U ü | 0.3 0.3 |
L大号 | 0.3 0.3 |
T吨 | 0.2 0.2 |
M米 | 0.1 0.1 |
I我 | 0.1 0.1 |
Is there any way to retain the duplicate order or at least when it finds a duplicate to put it past the one with the equal value?有没有办法保留重复的订单,或者至少在找到重复的订单时将其放在具有相同价值的订单之上?
Your code works fine, but you can simplify it as follows:您的代码工作正常,但您可以将其简化如下:
Source map:来源 map:
LinkedHashMap<Character, Double> tableProbability = new LinkedHashMap<>() {{ put('M', 0.1); put('U', 0.3); put('L', 0.3); put('T', 0.2); put('I', 0.1); }};
System.out.println(tableProbability); // {M=0.1, U=0.3, L=0.3, T=0.2, I=0.1}
This code works fine:此代码工作正常:
LinkedHashMap<Character, Double> sorted = new LinkedHashMap<>(); tableProbability.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).forEachOrdered(x -> sorted.put(x.getKey(), x.getValue()));
System.out.println(sorted); // {U=0.3, L=0.3, T=0.2, M=0.1, I=0.1}
Simplified version:简化版:
LinkedHashMap<Character, Double> sorted2 = tableProbability.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(LinkedHashMap::new, (col, e) -> col.put(e.getKey(), e.getValue()), HashMap::putAll);
System.out.println(sorted2); // {U=0.3, L=0.3, T=0.2, M=0.1, I=0.1}
See also: Ordering Map<String, Integer> by List<String> using streams另请参阅:使用流按 List<String> 排序 Map<String, Integer>
You may want to do it this way.你可能想这样做。 It is common operation.是普通操作。 If you want to return a TreeMap
, you can specify it below.如果要返回TreeMap
,可以在下面指定。 And normally one assigns to the interface type.并且通常一个分配给接口类型。 For a TreeMap
it would be NavigableMap
.对于TreeMap
,它将是NavigableMap
。
Map<Character, Double> sorted =
tableProbability.entrySet().stream()
.sorted(Map.Entry.comparingByValue(
Comparator.reverseOrder()))
.collect(Collectors.toMap(Entry::getKey,
Entry::getValue,
(a,b)->a, // merge, not used here but
// syntactically required
LinkedHashMap::new // type of map to return
));
Use a compound comparator:使用复合比较器:
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())
.andThen(Map.Entry.comparingByKey())
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.