知方号

知方号

java8 Stream对List的分组合并操作

话不多说,直接上代码,请朋友们自行测试~

public class Gamer47 {public static void main(String[] args) { //将List变成一个map mergeListmapToOnemap(null); //将两个List合并成一个List,“name”为map的key mergeTwoListmapToOneListmap(null,null,"name"); //对List分组统计 summaryGroup();}/** * 对List 进行分组合并,按某个相同的key进行合并,并sum某个key, * 类似单表group by 功能 */public static void summaryGroup(){ Map m1=new HashMap(); Map m11=new HashMap(); Map m12=new HashMap(); List list =new ArrayList(); List result =new ArrayList(); m1.put("aa", 11); m1.put("bb", "xm"); m1.put("cc", 122); m1.put("dd", 122); m11.put("aa", 12); m11.put("bb","xm"); m11.put("cc", 10); m11.put("dd", 122); m12.put("aa", 13); m12.put("bb", "zs"); m12.put("cc", 31); m12.put("dd", 122); list.add(m1); list.add(m11); list.add(m12); //按bb进行分组统计 Map glist = list.stream().collect(Collectors.groupingBy(e -> e.get("bb").toString())); glist.forEach((k,slist)->{ Map nmap=new HashMap(); IntSummaryStatistics sumcc = slist.stream().collect(Collectors.summarizingInt(e->Integer.valueOf(e.get("cc").toString()))); nmap.put("aa", slist.get(0).get("dd")); nmap.put("bb", slist.get(0).get("dd")); nmap.put("cc", sumcc.getSum());//求和 nmap.put("counts", slist.size());//计算 nmap.put("dd", slist.get(0).get("dd")); result.add(nmap); }); System.out.println("--------summaryGroup-------------"); result.forEach(x->{ System.out.println(x); }); }/** * list的中map合并为一个map,即List 转为Map newMap, * newMap中包含了list中每个map的key与value */public static void mergeListmapToOnemap(List listmap){ Map h1 = new HashMap(); h1.put("12","fdsa"); h1.put("123","fdsa"); h1.put("124","fdsa"); h1.put("125","fdsa"); Map h2 = new HashMap(); h2.put("h12","fdsa"); h2.put("h123","fdsa"); h2.put("h124","fdsa"); h2.put("h125","fdsa"); Map h3 = new HashMap(); h3.put("h12","fdsa"); h3.put("h3123","fdsa"); h3.put("h3124","fdsa"); h3.put("h3125","fdsa"); List lists = new ArrayList(); lists.add(h1); lists.add(h2); lists.add(h3); //用java 8 把lists里面的map合并成一个新的map: Map haNew = new HashMap(); // 包含了h1,h2,h3的内容 Map merged = lists.stream() .map(Map::entrySet) .flatMap(Set::stream) .distinct() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); Stream mlist = lists.stream().map(Map::entrySet); Stream fmlist = lists.stream() .map(Map::entrySet) .flatMap(Set::stream);System.out.println("merged="+merged);}/** * 两个list《map》中的map合并为一个list《map》,新的list中的每个map包含了之前的两个listmap的key */public static void mergeTwoListmapToOneListmap(List list1,List list2,final String mergeKey){ List lists = new ArrayList(); List lists1 = new ArrayList(); List lists2 = new ArrayList(); //--------------lists1-------------------- Map h1 = new HashMap(); h1.put("name","fdsa0"); h1.put("2","fdsa0"); h1.put("3","fdsa0"); h1.put("4","fdsa0"); Map h2 = new HashMap(); h2.put("name","fdsa00"); h2.put("2","fdsa00"); h2.put("3","fdsa00"); h2.put("4","fdsa00"); lists1.add(h1); lists1.add(h2); //--------------lists2-------------------- Map h3 = new HashMap(); h3.put("name","fdsa0"); h3.put("21","fdsa1"); h3.put("31","fdsa1"); h3.put("41","fdsa1"); Map h4 = new HashMap(); h4.put("name","fdsa00"); h4.put("21","fdsa2"); h4.put("31","fdsa2"); h4.put("41","fdsa2"); lists2.add(h3); lists2.add(h4); //测试 //mergeKey="name"; lists1.parallelStream().forEach(x->{ Map y2 = lists2.parallelStream().filter(y->y.get(mergeKey).toString().equals(x.get(mergeKey).toString())) .findFirst().get(); List sublist = Arrays.asList(x,y2); Map merged = sublist.stream() .map(Map::entrySet) .flatMap(Set::stream) .distinct() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); lists.add(merged); }); System.out.println("----------list--mergetMap---------"); lists.forEach(x->{ System.out.println(x); });}

另外一个例子:(把List里的数据,Value为Integer的值才相加,组合成一个新的Map)

public static void main(String[] args) { Map map1 = new HashMap(16); map1.put("姓名",1); map1.put("地址",2); map1.put("长相","好"); Map map2 = new HashMap(16); map2.put("姓名",3); map2.put("地址",4); map2.put("长相","好"); Map map3 = new HashMap(16); map3.put("姓名",5); map3.put("地址",6); map3.put("长相","好"); List list = new ArrayList(); list.add(map1); list.add(map2); list.add(map3); System.out.println(list); System.out.println(map1.entrySet()); Map collect = list .stream() .map(Map::entrySet) .flatMap(Set::stream) .filter(s -> s.getValue() instanceof Integer) .distinct() .collect( Collectors.toMap( Map.Entry::getKey, v -> (Integer) v.getValue(), Integer::sum ) ); System.out.println(collect); }

结果:

[{姓名=1, 地址=2, 长相=好}, {姓名=3, 地址=4, 长相=好}, {姓名=5, 地址=6, 长相=好}][姓名=1, 地址=2, 长相=好]{姓名=9, 地址=12}

 上面的例子我提及一点,我们来看看flatMap和Map区别:

Stream setStream = list .stream() .map(Map::entrySet);setStream.forEach(e -> System.out.print(e+ " , ")); System.out.println(""); Stream entryStream = list .stream() .map(Map::entrySet) .flatMap(Set::stream);entryStream.forEach(e -> System.out.print(e+ " , "));

结果:

[姓名=1, 地址=2, 长相=好] , [姓名=3, 地址=4, 长相=好] , [姓名=5, 地址=6, 长相=好]姓名=1 , 地址=2 , 长相=好 , 姓名=3 , 地址=4 , 长相=好 , 姓名=5 , 地址=6 , 长相=好

想要了解flatMap和map方法区别,请参看我的另外一篇博客:Java8的Stream Api 中的 map和 flatMap 操作方法 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。