话不多说,直接上代码,请朋友们自行测试~
public class Gamer47 { public static void main(String[] args) { //将List<Map>变成一个map mergeListmapToOnemap(null); //将两个List<Map>合并成一个List<Map>,“name”为map的key mergeTwoListmapToOneListmap(null,null,"name"); //对List<Map>分组统计 summaryGroup(); } /** * 对List<map> 进行分组合并,按某个相同的key进行合并,并sum某个key, * 类似单表group by 功能 */ public static void summaryGroup(){ Map<String,Object> m1=new HashMap<>(); Map<String,Object> m11=new HashMap<>(); Map<String,Object> m12=new HashMap<>(); List<Map<String,Object>> list =new ArrayList<>(); List<Map<String,Object>> 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<String, List<Map<String, Object>>> glist = list.stream().collect(Collectors.groupingBy(e -> e.get("bb").toString())); glist.forEach((k,slist)->{ Map<String,Object> 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> 转为Map newMap, * newMap中包含了list中每个map的key与value */ public static void mergeListmapToOnemap(List<Map> listmap){ Map<String,Object> h1 = new HashMap<>(); h1.put("12","fdsa"); h1.put("123","fdsa"); h1.put("124","fdsa"); h1.put("125","fdsa"); Map<String,Object> h2 = new HashMap<>(); h2.put("h12","fdsa"); h2.put("h123","fdsa"); h2.put("h124","fdsa"); h2.put("h125","fdsa"); Map<String,Object> h3 = new HashMap<>(); h3.put("h12","fdsa"); h3.put("h3123","fdsa"); h3.put("h3124","fdsa"); h3.put("h3125","fdsa"); List<Map<String,Object>> lists = new ArrayList<>(); lists.add(h1); lists.add(h2); lists.add(h3); //用java 8 把lists里面的map合并成一个新的map: Map<String,Object> haNew = new HashMap<>(); // 包含了h1,h2,h3的内容 Map<String, Object> merged = lists.stream() .map(Map::entrySet) .flatMap(Set::stream) .distinct() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); Stream<Object> mlist = lists.stream().map(Map::entrySet); Stream<Object> 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<Map> list1,List<Map> list2,final String mergeKey){ List<Map<String,Object>> lists = new ArrayList<>(); List<Map<String,Object>> lists1 = new ArrayList<>(); List<Map<String,Object>> lists2 = new ArrayList<>(); //--------------lists1-------------------- Map<String,Object> h1 = new HashMap<>(); h1.put("name","fdsa0"); h1.put("2","fdsa0"); h1.put("3","fdsa0"); h1.put("4","fdsa0"); Map<String,Object> 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<String,Object> h3 = new HashMap<>(); h3.put("name","fdsa0"); h3.put("21","fdsa1"); h3.put("31","fdsa1"); h3.put("41","fdsa1"); Map<String,Object> 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<String, Object> y2 = lists2.parallelStream().filter(y->y.get(mergeKey).toString().equals(x.get(mergeKey).toString())) .findFirst().get(); List<Map<String, Object>> sublist = Arrays.asList(x,y2); Map<String, Object> 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<Map<String,Object>>里的数据,Value为Integer的值才相加,组合成一个新的Map)
public static void main(String[] args) { Map<String, Object> map1 = new HashMap<>(16); map1.put("姓名",1); map1.put("地址",2); map1.put("长相","好"); Map<String, Object> map2 = new HashMap<>(16); map2.put("姓名",3); map2.put("地址",4); map2.put("长相","好"); Map<String, Object> map3 = new HashMap<>(16); map3.put("姓名",5); map3.put("地址",6); map3.put("长相","好"); List<Map<String,Object>> list = new ArrayList<>(); list.add(map1); list.add(map2); list.add(map3); System.out.println(list); System.out.println(map1.entrySet()); Map<String, Integer> 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<Set<Map.Entry<String, Object>>> setStream = list .stream() .map(Map::entrySet); setStream.forEach(e -> System.out.print(e+ " , ")); System.out.println(""); Stream<Map.Entry<String, Object>> 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 操作方法