The answer is given here , but since this is StackOverflow in Russian, I will provide a translation:
stream() , map() , collect() and all other stream methods are tied to generics. If you omit their use, all associated generics in the libraries will also be omitted and all structures will turn into regular interfaces without specifying the type. For example, if you use List<String> , then by calling the stream() method, get Stream<String> .
However, if you call stream() on an object of type List without specifying a type, then a Stream object will be returned.
The map() method in the same case has the following declaration:
<R> Stream<R> map(Function<? super T,? extends R> mapper)
But if we take into account the fact that the type was not specified, it will be reduced to the form:
Stream map(Function mapper)
Thus, as a result, map() will return a Stream object. This will also prevent the collect() function from working properly, which is of the form
<R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
Turn into:
Object collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner)
This will lose the List and List<String> declaration, respectively. Which causes a compilation error.
As a result, you can bring this object to the required form, however, this type of cast will cause a warning unchecked casts , because the compiler does not know whether you have led the type correctly.