java - 请问如何用集合循环存储键值对数据?
黄舟
黄舟 2017-04-18 09:55:34
0
3
860

我想做一个for循环存入一个键值对集合,但是由于map集合的特性,当存入第二个值的时候,第一个存入的值被覆盖了,请问如何处理?
问题示例代码如下:
HashMap map =new HashMap();
for(int i=0;i<2;i++){

  ArrayList  list =new ArrayList();
  list =getMethod(此方法返回一个list);
  map.put("示例",list);

}
请问这个方法怎么改list才能不被第二次循环覆盖掉呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
小葫芦

Either use a list instead of a map, or add a serial number to the key of the map.

巴扎黑

The map feature is like this. In fact, if your keys are all the same, just use List<List>; if they are not all the same and there are duplications, you can do what @scort said, or add another layer of Map< String,List<List>>

伊谢尔伦

When operating Value, determine whether the Key exists. When it exists, take out the Value object. You can use Liat here and then add it. The map is distinguished by Key

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template