简体   繁体   中英

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

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);   
        }
    }
}           

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()));

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.

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);
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM