Java 8: HashMap initialization with lambda expressions

前端 未结 3 808
面向向阳花
面向向阳花 2021-02-13 06:49

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:19

    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));
    

提交回复
热议问题