繁体   English   中英

如何将一个列表从 HashMap 添加到另一个列表

[英]How to add a list into another list from a HashMap

我正在尝试将 hashmap 个键和值存储到一个列表中,然后将该列表嵌套在另一个列表中。 所以像:

Hashmap 包含 - [ {3,1}, {2, 1}, {1,1} ]

我想将 {3,1}、{2、1}、{1,1} 分别添加到 3 个不同的列表中。

接下来,我会将 3 个列表添加到外部列表,将 3 个列表嵌套在内部列表中。

但我想了解为什么我下面的代码不起作用? 我下面的 nestedList 将引用我不理解的 tempList。

List<Integer> tempList = new ArrayList<>();
List<List<Integer>> nestedList = new ArrayList<>();
Map<Integer,Integer> map = new HashMap<>();
        
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
    tempList.clear(); //once it run this, nestedList will be cleared too. Why?
    tempList.add(entry.getKey());
    tempList.add(entry.getValue());
    nestedList.add(tempList); //tempList will get referenced to nestedList
}

Object类型在Java中是通过引用传递的,你需要修改你的代码如下:

List<List<Integer>> nestedList = new ArrayList<>();
Map<Integer,Integer> map = new HashMap<>();
    
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
    List<Integer> tempList = new ArrayList<>();
    tempList.add(entry.getKey());
    tempList.add(entry.getValue());
    nestedList.add(tempList); //tempList will get referenced to nestedList
}

尝试这个。

Map<Integer, Integer> map = Map.of(3, 1, 2, 1, 1, 1);
List<List<Integer>> nestedList = map.entrySet().stream()
    .map(e -> List.of(e.getKey(), e.getValue()))
    .toList();
System.out.println(nestedList);

output:

[[1, 1], [2, 1], [3, 1]]

如何将元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一个来自 dao 的列表,我想把这个列表放在一个 HashMap&gt; 中,我的列表可以包含一个服务,它有多个参数,比如 serviceId=3。 在我的最终 HashMap 中,结果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服务.java</p><pre> private int id; //Getters+Setters</pre><p> 参数.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 测试.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object>

[英]How to add element to hashmap of Object,List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从HashMap获取列表 <String,List<String> &gt;到另一个列表 如何将参数列表从JSON添加到HashMap? 如何在Java中以相同顺序将HashMap中的键添加到列表中 如何从一个自定义 object 列表中创建一个 HashMap,它将 String 作为键,值将是另一个 HashMap? 在HashMap上添加到列表值 如何添加项目以列出内部哈希图? 如何将空列表添加到 HashMap Java 中的值 如何添加与Hashmap中的键对应的列表元素? 如何将HashMap值添加到列表并进行打印 如何将元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一个来自 dao 的列表,我想把这个列表放在一个 HashMap&gt; 中,我的列表可以包含一个服务,它有多个参数,比如 serviceId=3。 在我的最终 HashMap 中,结果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服务.java</p><pre> private int id; //Getters+Setters</pre><p> 参数.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 测试.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM