普通写法
Map<String, List<Model>> map = new HashMap<>();
// 如何获取相同的num 的对象做运算
for (Model model6 : list) { String s = model6.getNum(); if (map.containsKey(s)) { map.get(s).add(model6); } else { ArrayList<Model> NumList = new ArrayList<Model>(); NumList.add(model6); map.put(s, NumList); } }
Java8写法
Map<String, List<Model>> result = list.stream().collect(Collectors.groupingBy(e -> e.getB()));