Java 8:使用lambda表达式初始化HashMap

use*_*443 14 java collections lambda hashmap java-8

我正在尝试一次声明和定义更大的哈希映射.我是这样做的:

public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{
    put(x, y);
    put(x, y);
}};
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在体内使用lambda表达式时put,我正在进行eclipse warrning/error.这就是我在HashMap中使用lambda的方法:

public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{
    put(0, () -> { return "nop"; });
    put(1, () -> { return "nothing...."; });
}};
Run Code Online (Sandbox Code Playgroud)

Eclipse以逗号开头强调lambda的整个部分.错误消息:

Syntax error on token ",", Name expected    
Syntax error on tokens, Expression expected instead
Run Code Online (Sandbox Code Playgroud)

有人知道我做错了什么吗?是否允许通过lambda表达式初始化HashMap?请帮忙.

Moh*_*lla 7

这在从以下网址下载的Netbeans Lamba版本中运行良好: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<Integer, Callable<String>> opcode_only = 
          new HashMap<Integer, Callable<String>>() {
            {
              put(0, () -> {
                return "nop";
              });
              put(1, () -> {
                return "nothing....";
              });
            }
          };
    System.out.println(opcode_only.get(0).call());
  }

}
Run Code Online (Sandbox Code Playgroud)