Java 8: HashMap initialization with lambda expressions

前端 未结 3 1956
情话喂你
情话喂你 2021-02-13 06:35

I\'m trying to declare and define larger hash map at once. This is how I do it:

public HashMap> opcode_only = new HashMap&l         


        
3条回答
  •  日久生厌
    2021-02-13 07:08

    You are doing correct, update JDK library to 1.8 version from Java Build Path in Eclipse Project properties.

    I just now tried the below code and it is working fine on my Eclipse:

            HashMap hmLambda = new HashMap() {
            {
                put(0, 1);
                put(1, 1);
            }
        };
        System.out.println(hmLambda.get(0));
    
        hmLambda.forEach((k, v) -> System.out.println("Key " + k
                + " and Values is: " + v));
    

提交回复
热议问题