Populating a hashmap with predefined values (java)

烂漫一生 提交于 2019-11-29 03:56:26

I would do the initialisation while setting up the HashMap

For example

private static final Map<String, String> m = new HashMap<String, String>() {{
    put("RC", "T1");
    put("AC", "T1");
}};

Then you wuld make sure that everything is set up together in your code.

I think @Nambari makes a good point though with perhaps having the value as a list rather than just a string. This does then swap your keys and values though.

eg

 private static final Map<String, List<String>> m = new HashMap<String, List<String>>() {{
    put("T1", Arrays.asList("RC", "AC");
}};

May be other way, List RC,AC,GH as value and T1 as key for hashmap, this way you can reduce number of entries in map.

You can use PropertiesConfiguration from apache commons.

value can contain value delimiters and will then be interpreted as a list of tokens. Default value delimiter is the comma ','. So the following property definition

key = This property, has multiple, values
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!