集合的Java8 IntStream不兼容的返回类型< Integer> .stream() [英] Java8 IntStream incompatible return type for Collections<Integer>.stream()

查看:241
本文介绍了集合的Java8 IntStream不兼容的返回类型< Integer> .stream()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点迷失了。我有一个代码(我没写),它有一个名为BitSetExt的类,它扩展了BitSet。签名如下所示:

I'm a bit lost with this. I have code (that I didn't write) which has a class called BitSetExt, which extends BitSet. The signature looks like:

private class BitSetExt extends BitSet implements Set<Integer>

在扩展类中不会覆盖stream()方法。我知道代码编译与Java 1.6完全一致。在使用Java8的Eclipse中,我收到错误:

The stream() method is not overridden in the extended class. I do know that the code compiles just fine with Java 1.6. In Eclipse with Java8, I get the error:

返回类型与继承的方法Collection.stream(),BitSet.stream()不兼容。

The return types are incompatible for the inherited methods Collection.stream(), BitSet.stream().

如果我尝试覆盖stream()并将IntStream返回类型更改为任何内容,我会得到一个不同的错误,并建议将返回类型更改为IntStream(显然不兼容) )。那么,我不理解什么以及如何修复此代码?

If I attempt to override stream() and change the IntStream return type to anything, I get a different error and a suggestion to change the return type to IntStream (which apparently isn't compatible). So, what am I not understanding and how can I fix this code?

感谢您的帮助。

推荐答案

从Java 8开始,BitSet的方法声明为

Since Java 8, BitSet has a method declared as

IntStream stream()

设置<整数> 有一个方法同名,声明为

and Set<Integer> has a method with the same name, declared as

Stream<Integer> stream()

由于这些方法具有相同的名称但返回类型不兼容,因此无法扩展BitSet并且同时实现Set。

Since those methods have the same name but an incompatible return type, it's impossible to extend BitSet and implement Set at the same time.

你必须重构该类,以便它不实现 Set< Integer> 以及,例如,添加一个方法,在对象上返回视图,实现 Set< Integer>

You'll have to refactor the class so that it doesn't implement Set<Integer> anymore and, for example, add a method which returns a view over the object, implementing Set<Integer>:

public Set<Integer> asSet();

这篇关于集合的Java8 IntStream不兼容的返回类型&lt; Integer&gt; .stream()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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