问题描述
我正在尝试按元素的频率对其进行排序
I am trying to sort elements by their frequency
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int itr = Integer.parseInt(br.readLine());
for (int i = 0; i < itr; i++) {
int n = Integer.parseInt(br.readLine());
String[] val = br.readLine().split(" ");
Map<Integer, Integer> map = new HashMap<>();
for (int j = 0; j < n; j++) {
Integer temp = Integer.parseInt(val[j]);
map.putIfAbsent(temp, 0);
map.put(temp, map.get(temp) + 1);
}
在这里,我根据频率对地图进行排序并将其存储为linkedHashMap
here I am sorting the map based on freq and storing it back as a linkedHashMap.
Map<Integer, Integer> sortedMap = map.entrySet().stream()
.sorted(
(Map.Entry.<Integer, Integer>comparingByValue())
.thenComparing(Map.Entry.comparingByKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
for(Map.Entry entries : sortedMap.entrySet()){
System.out.println(entries.getKey() + " " + entries.getValue());
}
}
}
}
以下引发编译器错误。
Map<Integer, Integer> sortedMap = map.entrySet().stream()
.sorted(
(Map.Entry.comparingByValue())
.thenComparing(Map.Entry.comparingByKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
样本输入
1
6
4 -2 10 12 -8 4
Sample input164 -2 10 12 -8 4
样本输出0
-8 -2 10 12 4 4
-8 -2 10 12 4 4
推荐答案
public static <K, V extends Comparable<? super V>>
Comparator<Map.Entry<K, V>> comparingByValue()
是静态方法,需要 K,V
可以在一个上下文中正确使用时正确设置/更正/调整通用上下文。否则,假定 K
是 Object
和 V
是可比较
,这不是 Stream< Map.Entry< Integer,Integer>> #sorted
所期望的。请注意,该流从 map.entrySet()
中获得了< Map.Entry< Integer,Integer>>
,其中映射
由< Integer,Integer>
或结果类型 sortedMap
,即 Map< Integer,Integer>
。
is a static method and it requires K, V
to set/correct/adjust the generic context correctly when it's used within one. Otherwise, it assumes K
is Object
and V
is Comparable
which is not what Stream<Map.Entry<Integer, Integer>>#sorted
expects. Note that the stream got <Map.Entry<Integer, Integer>>
from either map.entrySet()
, where the map
is parametrised by <Integer, Integer>
or the type of the result sortedMap
, which is Map<Integer, Integer>
.
Map.Entry。<整数,整数> comparingByValue()
给出了很好地解决它的必要提示。
Map.Entry.<Integer, Integer>comparingByValue()
gives the hints necessary to resolve it nicely.
<$ c照原样,$ c> Map.Entry.comparingByValue()是一件非常模糊的事情。
Map.Entry.comparingByValue()
, as is, is a very vague thing to write.
Comparator<Map.Entry<Object, Comparable<Comparable<?>>>>
comparator = comparingByValue();
当您给它一个通用的上下文时,它变得更加有意义。
It becomes more meaningful when you give it a generic context.
Comparator<Map.Entry<Integer, Integer>>
comparator = comparingByValue();
在您的情况下,这很重要,因为 Map.Entry.comparingByValue()
启动链,以下实例方法(例如 thenComparing
)将根据先前方法的通用参数来解析其自己的通用参数(此处为 comparingByValue
)。
In your case, it's important because Map.Entry.comparingByValue()
starts the chain, and the following instance methods (e.g. thenComparing
) will resolve their own generic parameters based on the generic parameters of the preceding method (here, comparingByValue
).
这篇关于< Integer,Integer>的重要性是什么?在Map.Entry中。< Integer,Integer> comparingByValue()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!