简体   繁体   English

使用流对 Map 进行排序抛出“无法调用”java.lang.Comparable.compareTo(Object) 流“

[英]Sorting an Map using streams throws"Cannot invoke "java.lang.Comparable.compareTo(Object) stream"

I have a HashMap with an Integer key and value.我有一个带有 Integer 键和值的 HashMap。 I am trying to sort this hashMap by values and store the keys of this sorted map in a list.我正在尝试按值对这个 hashMap 进行排序,并将这个排序的 map 的键存储在列表中。

I want to use stream to sort HashMap. Below is my code snippet我想用 stream 来排序 HashMap 下面是我的代码片段

Map<Integer, Integer> hm
            = new HashMap<Integer, Integer>();

int arr[]=new int[]{1,2,2,3,3,3,4,4,5};
n-arr.length;

    for (int i = 0; i < n; i++) {
        if(hm.get(arr[i])!=null)
            hm.put(arr[i], hm.get(arr[i]) + 1);
        else
            hm.put(arr[i],1);

List<Integer> temp= hm.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .map(entry -> entry.getKey())
                .collect(Collectors.toList());

How can I fix it?我该如何解决?

The error is thrown:抛出错误:

java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because the return value of "java.util.Map$Entry.getValue()" is null
  at line 541, java.base/java.util.Map$Entry.lambda$comparingByValue$1065357e$1
  at line 355, java.base/java.util.TimSort.countRunAndMakeAscending
  at line 220, java.base/java.util.TimSort.sort
  at line 1307, java.base/java.util.Arrays.sort
  at line 353, java.base/java.util.stream.SortedOps$SizedRefSortingSink.end
  at line 510, java.base/java.util.stream.AbstractPipeline.copyInto
  at line 499, java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto
  at line 921, java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential
  at line 234, java.base/java.util.stream.AbstractPipeline.evaluate
  at line 682, java.base/java.util.stream.ReferencePipeline.collect
  at line 16, Solution.topKFrequent
  at line 54, __DriverSolution__.__helper__
  at line 87, __Driver__.main

Your error says you have a null value in the map so that is what is causing the error.您的错误表明您在 map 中有一个 null 值,这就是导致错误的原因。 So either ensure there are no nulls when the map is constructed or filter them out as shown below.因此,要么确保在构造 map 时没有空值,要么如下所示过滤掉它们。

List<Integer> temp= hm.entrySet()
                .stream()
                .filter(e -> e.getValue() != null) // <-- added
                .sorted(Entry.comparingByValue())
                .map(Entry::getKey)
                .collect(Collectors.toList());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NullPointerException:尝试在 JSoup 库上调用接口方法“int java.lang.Comparable.compareTo(java.lang.Object)” - java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.Comparable.compareTo(java.lang.Object)' on JSoup Library Java可比对象排序:compareTo(Object)错误 - Java Comparable Object Sorting: compareTo(Object) error 在 Java 的泛型类中使用 compareTo 函数会导致 [Ljava.lang.Object; 不能转换为 [Ljava.lang.Comparable - Using compareTo function in a generic class in Java causes [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable java.lang.Comparable中的compareTo(K) <K> 不能应用于(java.lang.Comparable) - compareTo(K) in java.lang.Comparable<K> cannot be applied to (java.lang.Comparable) 收到可比较的Object []时找不到compareTo - Cannot find compareTo when receiving comparable Object[] java.lang.Object; 无法转换为[java.lang.comparable] - java.lang.Object; cannot be cast to [java.lang.comparable] 在Comparable中覆盖compareTo以进行排序 - Overiding compareTo in Comparable for sorting 错误:Car中的compareTo(Object)无法实现Comparable中的compareTo(T) - error: compareTo(Object) in Car cannot implement compareTo(T) in Comparable 使用Java比较按名称排序对象 - Sorting object by name using Java comparable 可比性:使用compareTo在各种参数上比较对象 - Comparable : Compare the object on various parameter using compareTo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM