前言
仅供学习参考,不保证性能问题
其中的实体类改成你自己的实体类
代码
/**
* 根据某个字段进行分组,分组后遍历方法
* <p>
* Map<String, List<MyDomain>> stringListMap = handleGroup(tmpList);
* Set<String> keySet = stringListMap.keySet();
* for (String key : keySet) {
* List<MyDomain> myDomains = stringListMap.get(key);
* }
*
* @param list
* @return
* @throws Exception
* @author DaenMax
*/
public static Map<String, List<PhcpSubmitContract>> handleGroup(List<PhcpSubmitContract> list) throws Exception {
Map<String, List<PhcpSubmitContract>> resultMap = new HashMap<>();
try {
for (PhcpSubmitContract obj : list) {
//分组依据key
String key = obj.getPlatNo();
if (resultMap.containsKey(key)) {
resultMap.get(key).add(obj);
} else {
List<PhcpSubmitContract> tmpList = new ArrayList<>();
tmpList.add(obj);
resultMap.put(key, tmpList);
}
}
} catch (Exception e) {
throw new Exception("分组时出现异常", e);
}
return resultMap;
}
标签:Exception,java,list,resultMap,tmpList,分组,key
From: https://www.cnblogs.com/daen/p/17089543.html