With JEP 101: Generalized Target-Type Inference, this
final List<Boolean> bools = Arrays.asList(true,false, true);
final List<Character> string = bools.stream()
.<Character>map(x -> x ? 'X' : 'O')
.collect(Collectors.<Character>toList());
should be reducable to
final List<Boolean> bools = Arrays.asList(true, false, true);
final List<Character> string = bools.stream()
.map(x -> x ? 'X' : 'O')
.collect(Collectors.toList());
in Java 8, but the latter does not compile:
Type mismatch: cannot convert from List<Object> to List<Character>
Have I got it wrong? Or am I ahead of my tools?
I am using JDK 8 build b120 together with eclipse-SDK-4.3.1-win32-x86_64-efx-0.9.0-SNAPSHOT.zip.
Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.
Type inference represents the Java compiler's ability to look at a method invocation and its corresponding declaration to check and determine the type argument(s). The inference algorithm checks the types of the arguments and, if available, assigned type is returned.
Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context. For example, the following example does not compile: List<String> list = new ArrayList<>(); list.
New FeaturesDefault method − Interface to have default method implementation. New tools − New compiler tools and utilities are added like 'jdeps' to figure out dependencies. Stream API − New stream API to facilitate pipeline processing. Date Time API − Improved date time API.
It just works fine under IntelliJ Idea 13 which seems ahead of Eclipse for Java8 support. So I guess you just have to wait until Eclipse will be able to compile this.
Seems like this issue is fixed now with the latest JDT snapshot implementing the desired proposal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With