首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Map套Map的应用

2013-04-02 
Map套Map的使用我遇到了这么个问题,不知道该怎么写好。有3个map:MapInteger, String m1 new HashMapIn

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"}}
求大神们指导啊!!!

[解决办法]
如果m1与m2中key值一样的话,就把相应的value组成新的键值对

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

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

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

热点排行