Java 8: HashMap initialization with lambda expressions

前端 未结 3 817
面向向阳花
面向向阳花 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:17

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

提交回复
热议问题