如何手动填写以下HashMap?
public static final HashMap<String,int[]> AGE_GROUPS = {"18-24",{18,24},
"25-29",{25,29},
"30-39",{30,39},
"40-49",{40,49},
"50-59",{50,59},
"60-69",{60,69},
"70-79",{70,79},
"80+",{80,120}};
Run Code Online (Sandbox Code Playgroud)
Ser*_*mir 10
这称为静态初始化.
private static final Map<Integer, String> myMap;
static {
Map<Integer, String> aMap = ....;
aMap.put(1, "one");
aMap.put(2, "two");
myMap = Collections.unmodifiableMap(aMap);
}
Run Code Online (Sandbox Code Playgroud)
在你的情况下;
public static final Map<String, int[]> AGE_GROUPS;
static{
Map<String, int[]> otherMap = new HashMap<String, int[]>();
otherMap.put( "10-20", new int[]{ 10, 11 } );
otherMap.put( "20-30", new int[]{ 20, 21 } );
AGE_GROUPS = Collections.unmodifiableMap( otherMap );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
893 次 |
| 最近记录: |