This is not a problem of streams. The error reflects the way that generic types were implemented in Java.
Java generic types are non-reifiable.
In Java, generic types have less type information available at run time than at compile time. This is so because generics are implemented in Java by erasure. Most of the generic type information exists only at compile time and is erased at run time.
For this reason, it is not possible to obtain a Class
object for any non-reifiable type. The type information is not available at run time.
Not only can you not get a Class
object for Stream<String>
, but you can't get a Class
object for any of T
, List<T>
, or List<String>
. These are all non-reifiable types.
The instanceof operator will only work with types from which a Class
object can be obtained. This is a good thing too, because List<T>
, List<String>
, and List<Integer>
all share the same Class object at run time. It would be confusing and error prone if the following snippet evaluated to true
:
List<String> strings = new ArrayList<>();
boolean b = strings instanceof List<Integer>;
System.out.println(b);
Instead, the Java compiler does not allow the use of the instanceof
operator on non-reifiable types.