The following code works fine in all online java compilers but eclipse throws compiler error. Is it a bug in eclipse or am I missing some setting somewhere? A simple fix to silence eclipse? online: https://ideone.com/l0bbhz. Note: This is a simplified cooked-up example to just point to the problem. I understand flatMap is not necessary in this case. In the actual case, I really need flatMap
package dummy;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
public class LearnJava {
public static void main(String[] args) {
String[] sa = {"ne", "two", "three"};
List<String> l = Arrays.stream(sa)
.flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
.collect(toList());
System.out.println(l.get(0));
}
}
Error in eclipse console.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from List<Object> to List<String>
at dummy.LearnJava.main(LearnJava.java:13)
My eclipse version:
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2) Build id: 20150219-0600
Update: I went with this minor workaround. It works without major refactoring!
.flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
To
.<String>flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
flatMapandCollections.singleton(s).stream().map(c -> c.toUpperCase())withmapands.toUpperCase()--this shouldn't make a difference re: type inference ontoList- obatakuflatMap(...). Writing[...].<String>flatMap(...)instead of[...].flatMap(...)makes Eclipse resolve the rest of the types just fine. - Turing85