集册 Java实例教程 使用diamond运算符进行构造函数类型推断

使用diamond运算符进行构造函数类型推断

欢马劈雪     最近更新时间:2020-01-02 10:19:05

499
使用diamond运算符进行构造函数类型推断

import java.util.ArrayList;

import java.util.List;

import java.util.Map;// 来自 NowJava.com


public class Main {

  @SuppressWarnings("unchecked")

  public static void main(String[] args) {


    List<String> list = new ArrayList<>();


    List<Map<String, List<String>>> stringList = new ArrayList<>();


    // There's More:Using <> when the type is not obvious
    /**来自 
     时代Java公众号**/

    List arrayList = new ArrayList<>();

    arrayList = new ArrayList<>();

    arrayList.add("First");

    arrayList.add("Second");


    arrayList = new ArrayList();

  }


  // //There's More: Understanding Erasure

  private static void useRawType() {

    List<String> arrayList = new ArrayList();

    arrayList.add("First");

    arrayList.add("Second");

    System.out.println(arrayList.get(0));

  }


  private static void useExplicitType() {

    List<String> arrayList = new ArrayList<String>();

    arrayList.add("First");

    arrayList.add("Second");

    System.out.println(arrayList.get(0));

  }


  
展开阅读全文