Java8 IntStream incompatible return type for Colle

2020-07-16 03:06发布

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>

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:

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

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?

Thanks for any help.

标签: java java-8
2条回答
成全新的幸福
2楼-- · 2020-07-16 03:13

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()

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.

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();
查看更多
女痞
3楼-- · 2020-07-16 03:33

That class will never compile in Java 8.

Set<Integer> requires that you implement a method with the signature

public Stream<Integer> stream();

while BitSet provides an implementation with the signature

public IntStream stream();

And an IntStream is not a subtype of Stream<integer>. There is no type that will satisfy both requirements.

查看更多
登录 后发表回答