我正在尝试在Java中实现数组哈希的散列,并认为如果我使用匿名等等就好了(我忘记了确切的用语/我不知道如何称呼它)。
HashMap<String, HashMap<String, String[]>> teams =
new HashMap<String, HashMap<String, String[]>>(){{
put("east", new HashMap<String, String[]>(){{
put("atlantic", new String[] { "bkn", "bos", "phi","tor", "ny" });
put("central", new String[] { "chi", "cle", "det", "ind", "mil" });
put("southeast", new String[] { "atl", "cha", "mia", "orl", "wsh" });
}});
put("west", new HashMap<String, String[]>(){{
put("northwest", new String[] { "den", "min", "okc", "por", "utah" });
put("pacific", new String[] { "gs", "lac", "lal", "phx", "sac" });
put("southwest", new String[] { "dal", "hou", "mem", "no", "sa" });
}});
}};
我的问题是,是否存在另一种考虑可读性的实现方式,或者可能完全改变了实现方式?
我知道Java不是正确的工具,但老板告诉我要这样做。
另外,请让我知道正确的用语。 TIA
最佳答案
只要我们不在乎运行速度,为什么不使用设计用来表示JSON等分层数据结构的语言? JAVA对它有强大的外部库支持...
杰森来救援!
@SuppressWarnings("unchecked")
HashMap teams =
new Gson().fromJson(
"{'east' : { 'atlantic' : ['bkn', 'bos', 'phi','tor', 'ny']," +
" 'central' : ['chi', 'cle', 'det', 'ind', 'mil']," +
" 'southeast' : ['atl', 'cha', 'mia', 'orl', 'wsh']}," +
" 'west' : { 'northwest' : ['den', 'min', 'okc', 'por', 'utah']," +
" 'pacific' : ['gs', 'lac', 'lal', 'phx', 'sac']," +
" 'southwest' : ['dal', 'hou', 'mem', 'no', 'sa']}}",
HashMap.class
);
http://code.google.com/p/google-gson/