Monday, June 27, 2011

JDK7 : Improved type inference for generic instance creation(diamond)

Note the constructor inference which is new to Java 7 also.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenericDiamondTypeInference {

    static class MyClass <X> {
        <Y> MyClass(Y y) {
        }
    }

    public static void main(String[] args) {

        // constructor inference
        // <X> = <Integer>, <Y> = <String>
        MyClass<Integer> myClass1 = new MyClass<>("");

        // standard stuff
        List<String> list = new ArrayList<>();
        Map<String, List<String>> map = new HashMap<>();

    }

}

No comments:

Post a Comment

Chitika