Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems understanding the Stream.generate static method signature in java 8

why java didn't choose this signature <T> Stream <T> Stream.generate (Supplier <? extends T> supplier) over this one <T> Stream <T> Stream.generate (Supplier <T> supplier) ?

I mean the below example (doesn't compile) is correct as a supplier of strings is also valid in a stream of charsequences no ?

Supplier <String> constantHello = () -> "Hello";

long count = Stream.<CharSequence>generate(constantHello).count();
like image 337
marsouf Avatar asked Aug 19 '17 15:08

marsouf


1 Answers

It's a bug. See https://bugs.openjdk.java.net/browse/JDK-8132097

It has been corrected in java 9. As you can see here, the method declaration is now

static <T> Stream<T> generate​(Supplier<? extends T> s)
like image 110
Misha Avatar answered Sep 19 '22 19:09

Misha