簡體   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