Java 8: HashMap initialization with lambda expressions

前端 未结 3 1974
情话喂你
情话喂你 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:15

    This works fine in the Netbeans Lamba builds downloaded from: http://bertram2.netbeans.org:8080/job/jdk8lambda/lastSuccessfulBuild/artifact/nbbuild/

    import java.util.*;
    import java.util.concurrent.Callable;
    
    public class StackoverFlowQuery {
    
      public static void main(String[] args) throws Exception {
    
        HashMap> opcode_only = 
              new HashMap>() {
                {
                  put(0, () -> {
                    return "nop";
                  });
                  put(1, () -> {
                    return "nothing....";
                  });
                }
              };
        System.out.println(opcode_only.get(0).call());
      }
    
    }
    

提交回复
热议问题