Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: Generic type inference improvements

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.

like image 282
Jens Piegsa Avatar asked Dec 14 '13 15:12

Jens Piegsa


People also ask

What is type inference in Java 8?

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.

What is type inference in generics?

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.

Does Java 7 support type inference?

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.

What are the Java 8 features introduce in interface concepts?

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.


2 Answers

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.

like image 71
FiNaLsPY Avatar answered Sep 27 '22 16:09

FiNaLsPY


Seems like this issue is fixed now with the latest JDT snapshot implementing the desired proposal.

like image 25
Jens Piegsa Avatar answered Sep 27 '22 17:09

Jens Piegsa