英文:
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<Long,List<Object[]>>, 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<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) {
	        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<Long, List<POJO>> map = new HashMap<>();
        Map<Long, List<POJO>> mapArray = result.stream()
                .collect(Collectors.groupingBy(POJO::getServiceId, Collectors.toList()));
答案2
得分: 0
不要重复使用相同的 ArrayList 引用。创建 new ArrayList<Object[]>();,并将 line 添加到其中。操作如下:
List result = dao.getServiceParam();
HashMap<Long,List<Object[]>> mapArray = new HashMap<Long, List<Object[]>>();
List<Object[]> listObj = null;
Object[] line = null, nextLine = null;
Long id = Long.valueOf(0), nextId = Long.valueOf(0);
if(!result.isEmpty()) {             
    for (int i=0; i< result.size(); i++) {
    	listObj = new ArrayList<Object[]>();
    	do{
	        line = (Object[])result.get(i);        
	        if ((BigDecimal) line[0]!=null) {	            
	            listObj.add(line);	
	            id = new Long(((BigDecimal) line[0]).longValue());              
	        }
	        i++;
	        if(i<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<Object[]>(); and add line to it. Do it as follows:
List result = dao.getServiceParam();
HashMap<Long,List<Object[]>> mapArray = new HashMap<Long, List<Object[]>>();
List<Object[]> listObj = null;
Object[] line = null, nextLine = null;
Long id = Long.valueOf(0), nextId = Long.valueOf(0);
if(!result.isEmpty()) {             
    for (int i=0; i< result.size(); i++) {
    	listObj = new ArrayList<Object[]>();
    	do{
	        line = (Object[])result.get(i);        
	        if ((BigDecimal) line[0]!=null) {	            
	            listObj.add(line);	
	            id = new Long(((BigDecimal) line[0]).longValue());              
	        }
	        i++;
	        if(i<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< 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);
      }
    }
  }
}
        
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论