Java将多个list对象根据属性分组后合并成一个新的集合
package com.lqm.collection; import java.util.*; import java.util.stream.Collectors; /** * @ProjectName: algorithm * @Package: com.*.collection * @ClassName: Test4 * @Author: *** * @Description: * @Date: 2021/5/19 9:47 * @Version: 1.0 */ public class Test4 { public static void main(String[] args) { List<TimeCount> list1 = new ArrayList<>(); TimeCount tc11 = new TimeCount("2021-01-06", 1); TimeCount tc12 = new TimeCount("2021-01-16", 1); list1.add(tc11); list1.add(tc12); List<TimeCount> list2 = new ArrayList<>(); TimeCount tc21 = new TimeCount("2021-01-11", 1); TimeCount tc22 = new TimeCount("2021-01-14", 1); TimeCount tc23 = new TimeCount("2021-01-28", 1); TimeCount tc24 = new TimeCount("2021-01-29", 1); TimeCount tc25 = new TimeCount("2021-01-30", 1); list2.add(tc21); list2.add(tc22); list2.add(tc23); list2.add(tc24); list2.add(tc25); List<TimeCount> list3 = new ArrayList<>(); TimeCount tc31 = new TimeCount("2021-01-06", 1); TimeCount tc32 = new TimeCount("2021-01-11", 1); TimeCount tc33 = new TimeCount("2021-01-28", 1); TimeCount tc34 = new TimeCount("2020-01-30", 1); list3.add(tc31); list3.add(tc32); list3.add(tc33); list3.add(tc34); Map<String, List> map = new HashMap<>(); for (String s : list1.stream().map(o -> o.getTime()).collect(Collectors.toList())) { map.put(s, null); } for (String s : list2.stream().map(o -> o.getTime()).collect(Collectors.toList())) { map.put(s, null); } for (String s : list3.stream().map(o -> o.getTime()).collect(Collectors.toList())) { map.put(s, null); } for (String s : map.keySet()) { List result = new ArrayList(); List<TimeCount> t1 = list1.stream().filter(o -> o.getTime().equals(s)).collect(Collectors.toList()); if (t1.size() == 0) { result.add(0); } else { result.add(t1.get(0).getCount()); } List<TimeCount> t2 = list2.stream().filter(o -> o.getTime().equals(s)).collect(Collectors.toList()); if (t2.size() == 0) { result.add(0); } else { result.add(t2.get(0).getCount()); } List<TimeCount> t3 = list3.stream().filter(o -> o.getTime().equals(s)).collect(Collectors.toList()); if (t3.size() == 0) { result.add(0); } else { result.add(t3.get(0).getCount()); } map.put(s, result); }
//按照map中的key进行升序排序 Map<String, List> sortedMap = map.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -> oldVal, LinkedHashMap::new ) ); sortedMap.forEach((k, v) -> System.out.println("key:" + k + "value:" + v)); } }
package com.lqm.collection; /** * @ProjectName: algorithm * @Package: com.*.collection * @ClassName: TimeCount * @Author: *** * @Description: * @Date: 2021/5/19 10:07 * @Version: 1.0 */ public class TimeCount { private String time; private Integer count; public TimeCount(String time, Integer count) { this.time = time; this.count = count; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } @Override public String toString() { return "TimeCount{" + "time='" + time + '\'' + ", count=" + count + '}'; } }
输出结果:
key:2020-01-30value:[0, 0, 1] key:2021-01-06value:[1, 0, 1] key:2021-01-11value:[0, 1, 1] key:2021-01-14value:[0, 1, 0] key:2021-01-16value:[1, 0, 0] key:2021-01-28value:[0, 1, 1] key:2021-01-29value:[0, 1, 0] key:2021-01-30value:[0, 1, 0]