日期:2014-05-20  浏览次数:20676 次

Map套Map的使用
我遇到了这么个问题,不知道该怎么写好。
有3个map:
Map<Integer, String> m1 = new HashMap<Integer, String>();
Map<Integer, String> m2 = new HashMap<Integer, String>();
Map<Integer, Map<String, String>> map1 = new HashMap<Integer, Map<String, String>>();
m1.put(111, "1111");
m1.put(222, "2222");
m1.put(333, "3333");
m2.put(111, "aaaa");
m2.put(222, "bbbb");
m2.put(333, "cccc");
---------------------------------------------------
map1里是map又嵌套了一层map,我想这样,如果m1与m2中key值一样的话,就把相应的value组成新的键值对,放到map1里的map。
我最后想得到map1的结果是{111={"1111", "aaaa"}, 222={"2222", "bbbb"}, 333={"3333", "cccc"}}
求大神们指导啊!!!
hashmap string integer

------解决方案--------------------
给你个map套list的


------解决方案--------------------
如果m1与m2中key值一样的话,就把相应的value组成新的键值对

这样的设计本身就有问题。要实现的可以理解为“一个KEY对应多个VALUE”。

可以将多个值作为长字符串拼接。

如果真要这么做。使用Map<Integer, Object>或许能有意外收获。