Dam*_*mir 23 java collections initialization
可能重复:
如何在Java中初始化静态地图
如何在初始化时用Java填充HashMap,这可能是这样的吗?
public static Map<byte,int> sizeNeeded=new HashMap<byte,int>(){1,1};
Run Code Online (Sandbox Code Playgroud)
Jig*_*shi 57
byte,int是原始的,collection在object上工作.你需要这样的东西
public static Map<Byte, Integer> sizeNeeded = new HashMap<Byte, Integer>() {
{
put(new Byte("1"), 1);
put(new Byte("2"), 2);
}
;
};
Run Code Online (Sandbox Code Playgroud)
这将创建一个新的映射并使用初始化块,它将调用put方法来填充数据