Java HashMap:如何为一个键添加多个值?

huangapple go评论75阅读模式
英文:

Java HashMap : How to add multiple values to one key?

问题

从dao获取了一个列表,我想把这个列表放入一个 `HashMap<Long, List<Object[]>>` 中,我的列表可以包含一个带有多个参数的服务,比如 `serviceId=3`。
在我的最终HashMap中,结果看起来像这样: `{1, [100], 2, [101], 3, [[102, B], [103, B], [104, C]]}`。我尝试了这段代码,但它没有起作用。

    serviceId   paramId   type
        1         100      A
        2         101      A
        3         102      B
        3         103      B
        3         104      C

代码:

    List result = dao.getServiceParam();
    HashMap<Long, List<Object[]>> mapArray = new HashMap<Long, List<Object[]>>();
    List<Object[]> listObj = new ArrayList<Object[]>();
    if (!result.isEmpty()) {
        for (int i = 0; i < result.size(); i++) {
            Object[] line = (Object[]) result.get(i);
            if ((BigDecimal) line[0] != null) {
                listObj.add(line);
                mapArray.put(new Long(((BigDecimal) line[0]).longValue()), listObj);
            }
        }
    }
英文:

I have a list from dao, I want to put this list in a HashMap&lt;Long,List&lt;Object[]&gt;&gt;, my list can contain a service which have multiple parameters like the serviceId=3.
In my final HashMap, the result looks like : {1,[100],2[101],3=[[102,B],[103,B],[104,C]]}. I tried with this code but it didn't work.

serviceId   paramId   type
    1         100      A
    2         101      A
    3         102      B
    3         103      B
    3         104      C

Code:

List result = dao.getServiceParam();
HashMap&lt;Long,List&lt;Object[]&gt;&gt; mapArray = new HashMap&lt;Long, List&lt;Object[]&gt;&gt;();
List&lt;Object[]&gt; listObj = new ArrayList&lt;Object[]&gt;();
if(!result.isEmpty()) {				
	for (int i=0; i&lt; result.size(); i++) {
	    Object[] line = (Object[])result.get(i);		
        if ((BigDecimal) line[0]!=null) {
	        istObj.add(line);
	        mapArray .put(new Long(((BigDecimal) line[0]).longValue()), listObj);	
        }
    }
}			

答案1

得分: 0

代替使用对象数组,创建您自己的POJO来表示数据,然后将其存储在列表中。然后,我们可以使用Collectors#grouping按serviceId进行分组,然后Collect到一个列表中的POJO对象列表。

class POJO {
    private final long serviceId;

    private final int paramId;

    private final String type;

    // 构造函数,获取器
}
Map<Long, List<POJO>> map = new HashMap<>();
Map<Long, List<POJO>> mapArray = result.stream()
        .collect(Collectors.groupingBy(POJO::getServiceId, Collectors.toList()));
英文:

Instead of using an array of Object, create your own POJO to represent the data then store it in a list. We can then take the List of POJO objects and create a map using Collectors#grouping to group by the serviceId and then Collect to a List.

class POJO {
    private final long serviceId;

    private final int paramId;

    private final String type;

    // constructor, getters
}
Map&lt;Long, List&lt;POJO&gt;&gt; map = new HashMap&lt;&gt;();
        Map&lt;Long, List&lt;POJO&gt;&gt; mapArray = result.stream()
                .collect(Collectors.groupingBy(POJO::getServiceId, Collectors.toList()));

答案2

得分: 0

不要重复使用相同的 ArrayList 引用。创建 new ArrayList&lt;Object[]&gt;();,并将 line 添加到其中。操作如下:

List result = dao.getServiceParam();
HashMap&lt;Long,List&lt;Object[]&gt;&gt; mapArray = new HashMap&lt;Long, List&lt;Object[]&gt;&gt;();
List&lt;Object[]&gt; listObj = null;
Object[] line = null, nextLine = null;
Long id = Long.valueOf(0), nextId = Long.valueOf(0);
if(!result.isEmpty()) {             
    for (int i=0; i&lt; result.size(); i++) {
    	listObj = new ArrayList&lt;Object[]&gt;();
    	do{
	        line = (Object[])result.get(i);        
	        if ((BigDecimal) line[0]!=null) {	            
	            listObj.add(line);	
	            id = new Long(((BigDecimal) line[0]).longValue());              
	        }
	        i++;
	        if(i&lt;result.size()){
		        nextLine = (Object[])result.get(i);        
		        if ((BigDecimal) nextLine[0]!=null) {	            
		            nextId = new Long(((BigDecimal) nextLine[0]).longValue());              
		        }
	        } 
	    }while(id.equals(nextId));    
	    mapArray.put(id, listObj);
	    i--;
    }
}

如有疑问或问题,请随时进行评论。

英文:

Do not reuse the same reference of ArrayList. Create new ArrayList&lt;Object[]&gt;(); and add line to it. Do it as follows:

List result = dao.getServiceParam();
HashMap&lt;Long,List&lt;Object[]&gt;&gt; mapArray = new HashMap&lt;Long, List&lt;Object[]&gt;&gt;();
List&lt;Object[]&gt; listObj = null;
Object[] line = null, nextLine = null;
Long id = Long.valueOf(0), nextId = Long.valueOf(0);
if(!result.isEmpty()) {             
    for (int i=0; i&lt; result.size(); i++) {
    	listObj = new ArrayList&lt;Object[]&gt;();
    	do{
	        line = (Object[])result.get(i);        
	        if ((BigDecimal) line[0]!=null) {	            
	            listObj.add(line);	
	            id = new Long(((BigDecimal) line[0]).longValue());              
	        }
	        i++;
	        if(i&lt;result.size()){
		        nextLine = (Object[])result.get(i);        
		        if ((BigDecimal) nextLine[0]!=null) {	            
		            nextId = new Long(((BigDecimal) nextLine[0]).longValue());              
		        }
	        } 
	    }while(id.equals(nextId));    
	    mapArray.put(id, listObj);
	    i--;
    }
}

Feel free to comment in case of any doubt/issue.

答案3

得分: 0

if (!result.isEmpty()) {
    for (int i = 0; i < result.size(); i++) {
        Object[] line = (Object[]) result.get(i);
        if (((BigDecimal) line[0]) != null) {
            Long longVal = new Long(((BigDecimal) line[0]).longValue());
            if (mapArray.containsKey(longVal)) {
                List<Object[]> tempList = mapArray.get(longVal);
                tempList.add(line);
                mapArray.put(longVal, tempList);
            } else {
                List<Object[]> tempList = new ArrayList<>();
                tempList.add(line);
                mapArray.put(longVal, tempList);
            }
        }
    }
}
英文:
if(!result.isEmpty()) {             
 for (int i=0; i&lt; result.size(); i++) {
  Object[] line=(Object[])result.get(i);     
      if((BigDecimal) line[0]!=null) {
      Long longVal = new Long(((BigDecimal) line[0]).longValue());
       if(mapArray.containsKey(longVal){
         List&lt;Object[]&gt; tempList = mapArray.get(longVal);
         tempList.add(line);
         mapArray.put(longVal, tempList); 
      }
       else{
         List&lt;Object[]&gt; tempList = new ArrayList&lt;&gt;();
         tempList.add(line);
         mapArray.put(longVal,tempList);
      }
    }
  }
}
        

huangapple
  • 本文由 发表于 2020年4月6日 20:16:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61059660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定