< Integer,Integer>的重要性是什么?在Map.Entry中。< Integer,Integer> comparingByValu

编程入门 行业动态 更新时间:2024-10-28 06:31:54
本文介绍了< Integer,Integer>的重要性是什么?在Map.Entry中。< Integer,Integer> comparingByValue()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试按元素的频率对其进行排序

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.EntryparingByKey())) .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.EntryparingByValue()) .thenComparing(Map.EntryparingByKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

样本输入 1 6 4 -2 10 12 -8 4

Sample input 1 6 4 -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.

Map.EntryparingByValue()是一件非常模糊的事情。

Map.EntryparingByValue(), 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.EntryparingByValue()启动链,以下实例方法(例如 thenComparing )将根据先前方法的通用参数来解析其自己的通用参数(此处为 comparingByValue )。

In your case, it's important because Map.EntryparingByValue() 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).

更多推荐

&lt; Integer,Integer&gt;的重要性是什么?在Map.Entry中。&lt; Integer,Integer&

本文发布于:2023-09-09 17:58:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466860.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重要性   Integer   amp   lt   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!