JDK8 根据某个字段或多个字段将实体类List分组
一、分组(单个字段) 1、分组:Map<Integer,List<BaseOrganizationVO>> iuIdMap = list.stream().collect(Collectors.groupingBy(BaseOrganizationVO :: getOrgId)); 2、取值: if(!ObjectUtils.isEmpty(iuIdMap.get(orderHeaderVO.getOuId()))){//判断orderHeaderVO.getOuId()是否存在于iuIdMap的键值中 BaseOrganizationVO baseOrganizationVO = iuIdMap.get(orderHeaderVO.getOuId()).get(0);//获取value[0] } 二、分组(多个字段,用下划线拼接) 1、分组: Map<String,List<SrmPurRequisitionLineVO>> requisitionLineMap=requisitionLineVOList.stream().collect(Collectors.groupingBy(o->{ StringBuffer bf = new StringBuffer(); bf.append(o.getOuId()).append("_"); bf.append(o.getIuId()).append("_"); bf.append(o.getSupplierId()).append("_"); bf.append(o.getProofingType()).append("_"); bf.append(o.getBuyerId()); return bf.toString(); })); 2、取值 for(Map.Entry<String,List<SrmPurRequisitionLineVO>> entry:requisitionLineMap.entrySet()){ String keyString= entry.getKey();//键值 List<SrmPurRequisitionLineVO> purRequisitionLineVOList=entry.getValue();//value }