gpt4 book ai didi

java - 如何手动填写HashMap?

转载 作者:行者123 更新时间:2023-12-02 04:59:27 26 4
gpt4 key购买 nike

如何手动填写下面的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}};

最佳答案

这称为静态初始化。

 private static final Map<Integer, String> myMap;
static {
Map<Integer, String> aMap = ....;
aMap.put(1, "one");
aMap.put(2, "two");
myMap = Collections.unmodifiableMap(aMap);
}

根据您的情况;

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

}

关于java - 如何手动填写HashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32391393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com