Accepted answer


  • your code is invalid.
  • oxygen's java compiler incorrectly allows your code to compile.
  • photon's java compiler correctly reports an error with your code.

concat() will return a stream of a type which is the closest match for the types of the streams being concatenated. for example, if you concat() a stream<int> with a stream<long> then concat() will return a stream<number>.

and if you concat() two streams whose elements bear no relationship with each other, such as stream<string> and stream<long>, then concat() will return a stream<object>. that is what happens in your code, since myclass and myinterface bear no relationship to each other, apart from having object as a parent.

it looks like something was implicitly casting the return type of filter to stream which then lead to stream.concat returning the expected type.

that explanation looks tempting since it appears to fit the facts, but what happens if the predicate in your filter is changed to test for runnable instead of myinterface?

stream<myinterface> concatenation2 = stream.concat(
    myclassstream.filter(element -> element instanceof runnable),

the code still compiles and the tooltip is unchanged, so the filtering is clearly not impacting the return type. put another way, the first parameter to concat() will return a stream<myclass> regardless of what is done in filter(). the fact that your filter guaranteed that it would also only return myinterface elements is not relevant, but it appeared to be significant because the type of the variable receiving the stream from concat() was stream<myinterface>.

and what happens if the type of the variable receiving the stream from concat() is changed from myinterface to something absurd and meaningless such as deque<localdatetime>?...

stream<deque<localdatetime>> concatenation3 = stream.concat(
    myclassstream.filter(element -> element instanceof myinterface),

two things are notable:

  • even though that code is obviously invalid, it still compiles on oxygen (but not on photon).
  • the tooltip for concat() now shows its return type to be stream<deque<localdatetime>> which clearly makes no sense.

why did this code break? how could i get previous behavior?

oxygen's compiler was allowing invalid variable definitions using stream.concat(), and it seems that this was fixed in photon. you shouldn't want that previous behavior since it was incorrect.

Related Query

More Query from same tag