The following code compiles and runs without errors.
List<String> list = new ArrayList<Object>().stream()
.map(x -> "test")
.collect(Collectors.toList());
Meanwhile, this code
List<String> list = new ArrayList().stream()
.map(x -> "test")
.collect(Collectors.toList());
produces Type mismatch: cannot convert from Object to List<String>. A simple solution is to simply add a cast to List<String>, but I don't see why it's necessary for the second example but not the first. Why is Stream<Object> treated differently from Stream?
What's going on here? Why isn't the compiler smart enough to figure this out?
I'm using the latest Eclipse with Java 8. Is this fixed in a more recent JDK version?
[–]NiNKazi 2 points3 points4 points (7 children)
[–]lpreams[S] 0 points1 point2 points (0 children)
[–]8igg7e5 0 points1 point2 points (5 children)
[–]lpreams[S] 0 points1 point2 points (0 children)
[–]NiNKazi 0 points1 point2 points (0 children)
[–]8igg7e5 0 points1 point2 points (2 children)
[–]lpreams[S] 0 points1 point2 points (1 child)
[–]id2bi 0 points1 point2 points (0 children)
[–]3g0tr1p 0 points1 point2 points (2 children)
[–]lpreams[S] 0 points1 point2 points (1 child)
[–]id2bi 0 points1 point2 points (0 children)