简体   繁体   中英

Java8 IntStream incompatible return type for Collections<Integer>.stream()

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM