Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

java-8

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.

like image 643
Wayne Y Avatar asked May 25 '15 20:05

Wayne Y


2 Answers

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();
like image 147
JB Nizet Avatar answered Oct 04 '22 16:10

JB Nizet


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.

like image 27
Sotirios Delimanolis Avatar answered Oct 04 '22 16:10

Sotirios Delimanolis