在Java中的“哈希表套哈希表套数组”

3

我正在尝试在Java中实现哈希的哈希数组,并且认为如果我使用匿名blah blah(我忘记了确切的术语/我不知道如何称呼它),那将是很好的。

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 不是正确的工具,但是我的老板让我这样做。 还请告诉我正确的术语。谢谢!


你是不是想说“匿名内部类”? - SJuan76
也许你想把这个问题放在codereview.stackexchange.com上。 - durron597
@durron597 我马上要这样做,但我想知道其他的选择。我会更新我的问题,谢谢。 - jchips12
@SJuan76 我不确定,我倾向于匿名对象。有人可以确认一下SJuan76的答案吗?谢谢! - jchips12
我在这里会做的主要不同之处(我不想把它作为答案,但如果你非常喜欢,我可以)是将这些城市存储在文件中或者更好的是某种SQL数据库中。这样,如果它发生变化,您就不必更改应用程序的源代码。 - durron597
2个回答

3
只要我们不在意运行速度,为什么不使用专门用于表达分层数据结构的语言,比如JSON呢?JAVA有很好的外部库支持它...
Gson来拯救!
    @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/


我最终决定使用Gson和一个json文件(由@durron597建议)一起使用。 - jchips12

2

使用辅助方法

private void addTeams(String area, String codes) {
    String[] areas = area.split("/");
    Map<String, String[]> map = teams.get(areas[0]);
    if (map == null) teams.put(areas[0], map = new HashMap<String, String[]>());
    map.put(areas[1], codes.split(", ?"));
}

Map<String, Map<String, String[]>> teams = new HashMap<String, Map<String, String[]>>();{
    addTeams("east/atlantic", "bkn, bos, phi, tor, ny");
    addTeams("east/central", "chi, cle, det, ind, mil");
    addTeams("east/southeast", "atl, cha, mia, orl, wsh");
    addTeams("west/northwest", "den, min, okc, por, utah");
    addTeams("west/pacific", "gs, lac, lal, phx, sac");
    addTeams("west.southwest", "dal, hou, mem, no, sa");
}

你可以替换

您可以更改

new String[] { "bkn", "bos", "phi","tor", "ny" }

使用

"bkn,bos,phi,tor,ny".split(",");

1
我不建议这样做。这会增加额外的工作量,而收益却很少。 - gbtimmon
1
我不喜欢这个快捷方式,因为更容易出现打字错误。"foo,bar.baz".split(","); 在编译时可以通过,但在运行时会失败,而 new String[]{"foo", "bar". "baz"}; 会引发编译器错误。不值得为了省下几个按键而冒这个风险。 - atamanroman
1
为什么不使用可变参数呢?private void addTeams(String area, String... codes); :) - atamanroman
@atamanroman "foo, bar.bar""foo", "bar.bar"都可以编译。你可以使用可变参数,但为了获得很少的优势而更长时间地键入。 - Peter Lawrey
@atamanroman 显然,您不会选择出现在内容中的分隔符。它可以是任何您喜欢的东西。 - Peter Lawrey
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接