I'm trying to declare and define larger hash map at once. This is how I do it:
public HashMap> opcode_only = new HashMap>() {{
put(x, y);
put(x, y);
}};
But, when I try to use lambda expressions in body of put, I'm hitting on eclipse warrning/error. This is how I use lambda in HashMap:
public HashMap> opcode_only = new HashMap>() {{
put(0, () -> { return "nop"; });
put(1, () -> { return "nothing...."; });
}};
Eclipse underlines whole part of lambda starting with comma before.
Error messages:
Syntax error on token ",", Name expected
Syntax error on tokens, Expression expected instead
Does anybody know what am I doing wrong? Is initialization by lambda expression allowed in HashMap? Please help.
解决方案
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());
}
}