推理变量K具有不兼容的范围 [英] inference variable K has incompatible bounds

查看:275
本文介绍了推理变量K具有不兼容的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对上述问题写解决方案

I was trying to write a solution for the problem stated here as Stuart Marks pointed out to use a helper class. I got stuck on the code due to this error:

Test.java:27: error: incompatible types: inference variable K has incompatible bounds                                                                                                                          
         .collect(Collectors.groupingBy(p -> p.getT2()));                                                                                                                                                      
                 ^                                                                                                                                                                                             
    equality constraints: Tuple                                                                                                                                                                                
    lower bounds: Integer                                                                                                                                                                                      
  where K,T are type-variables:                                                                                                                                                                                
    K extends Object declared in method <T,K>groupingBy(Function<? super T,? extends K>)                                                                                                                       
    T extends Object declared in method <T,K>groupingBy(Function<? super T,? extends K>) 

我当前的代码:

import java.util.stream.IntStream;
import java.util.stream.Collectors;
import java.util.Map;

public class Test{

    private static final class Tuple {
        private final Integer t1;
        private final Integer t2;

        private Tuple(final Integer t1, final Integer t2) {
            this.t1 = t1;
            this.t2 = t2;
        }

        public Integer getT1() { return t1; }
        public Integer getT2() { return t2; }
    }

     public static void main(String []args){
        System.out.println("Hello World");

        Map<Tuple, Integer> results =
        IntStream.range(1, 10).boxed()
         .map(p -> new Tuple(p, p % 2))                     // Expensive computation
         .filter(p -> p.getT2() != 0)
         .collect(Collectors.groupingBy(p -> p.getT2())); 

        results.forEach((k, v) -> System.out.println(k + "=" + v));
     }

}

我本人对Java 8还是很陌生,解决方案可能是错误的,但是我不知道该错误意味着什么或如何解决. Tuple类也曾经是通用的,但是由于我认为这是导致问题的原因,因此我删除了通用类型,但仍然存在相同的错误.

I'm quite new to Java 8 myself, the solution might be wrong but I have no clue what the error means or how to resolve it. The Tuple class used to be generic as well but since I thought that caused the problem I removed the generic types but the same error remained.

推荐答案

collect()groupingBy()不会执行您认为的操作.您目前的作业结果类型为:

collect() and groupingBy() do not do what you think they do. The result type of your assignment as it is right now is:

Map<Integer, List<Tuple>> results = ...

换句话说,您要按p.getT2()(Integer)分组,然后在每个组中,将该组的元组收集到List<Tuple>中,结果如下:

In other words, you're grouping by p.getT2() (Integer) and in each group, collect the tuples of that group into a List<Tuple>, resulting in something like:

1=[Test$Tuple@7699a589, Test$Tuple@58372a00, Test$Tuple@4dd8dc3, 
   Test$Tuple@6d03e736, Test$Tuple@568db2f2]

这篇关于推理变量K具有不兼容的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆