• JDK1.8 Stream之toMap和groupingBy全参数的使用


    现在有一个商品地址对象的集合List<ProductAddress> productAddresses,该集合中productId和addressId是一对多的关系

     1 @Data
     2 @AllArgsConstructor
     3 class ProductAddress {
     4     private int productId;
     5 
     6     private int addressId;
     7 
     8     private static List<ProductAddress> productAddresses;
     9 
    10     static {
    11         productAddresses = new ArrayList<>();
    12         productAddresses.add(new ProductAddress(1001, 1));
    13         productAddresses.add(new ProductAddress(1001, 2));
    14         productAddresses.add(new ProductAddress(1001, 3));
    15         productAddresses.add(new ProductAddress(1002, 3));
    16         productAddresses.add(new ProductAddress(1002, 4));
    17         productAddresses.add(new ProductAddress(1003, 5));
    18     }
    19 }

    需求

    以productId分组变成一个Map<Integer,List<ProductAddress>>

    传统方法

     1 public static void main(String[] args) {
     2     Map<Integer, List<ProductAddress>> map = new HashMap<>();
     3     for (ProductAddress productAddress : productAddresses) {
     4         if (map.get(productAddress.getProductId()) == null) {
     5             map.put(productAddress.getProductId(), new ArrayList<>(Arrays.asList(productAddress)));
     6         } else {
     7             map.get(productAddress.getProductId()).add(productAddress);
     8         }
     9     }
    10     System.out.println(JSONObject.toJSON(map));
    11 }

    lambda表达式实现

    1 public static void main(String[] args) {
    2     Map<Integer, List<ProductAddress>> map = productAddresses.stream().collect(Collectors.groupingBy(ProductAddress::getProductId));
    3     System.out.println(JSONObject.toJSON(map));
    4 }

    新的需求

    以productId分组变成一个Map<Integer,List<Integer>>,value是其对应的addressId的集合

    传统方法

     1 public static void main(String[] args) {
     2     Map<Integer, List<Integer>> map = new HashMap<>();
     3     for (ProductAddress productAddress : productAddresses) {
     4         if (map.get(productAddress.getProductId()) == null) {
     5             map.put(productAddress.getProductId(), new ArrayList<>(Arrays.asList(productAddress.getAddressId())));
     6         } else {
     7             map.get(productAddress.getProductId()).add(productAddress.getAddressId());
     8         }
     9     }
    10     System.out.println(JSONObject.toJSON(map));
    11 }

    lambda表达式实现

    首先想到的是使用toMap把value转成list并使用toMap提供的key冲突解决办法。

     1 public static void main(String[] args) {
     2     Map<Integer, List<Integer>> map = productAddresses.stream()
     3             .collect(Collectors.groupingBy(ProductAddress::getProductId, HashMap::new,
     4                     Collectors.mapping(ProductAddress::getAddressId, Collectors.toList())));
     5     productAddresses.stream().collect(Collectors.toMap(ProductAddress::getProductId, v -> new ArrayList(Arrays.asList(v.getAddressId())), (existValue, newValue) -> {
     6         existValue.addAll(newValue);
     7         return existValue;
     8     }));
     9     System.out.println(map);
    10 }

    但感觉这个写法看起来怪怪的,琢磨groupingBy发现,使用groupingBy的后两个参数完成对象到具体字段的映射可以优雅实现

    1 public static void main(String[] args) {
    2     Map<Integer, List<Integer>> map = productAddresses.stream()
    3             .collect(Collectors.groupingBy(ProductAddress::getProductId, HashMap::new,
    4                     Collectors.mapping(ProductAddress::getAddressId, Collectors.toList())));
    5     System.out.println(map);
    6 } 
  • 相关阅读:
    FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具
    如何去设计一个自适应的网页设计或HTMl5
    教育行业SaaS选型 需要注意的三点问题
    SaaS系列介绍之十五: SaaS知识重用
    SaaS系列介绍之十四: SaaS软件开发分析
    SaaS系列介绍之十三: SaaS系统体系架构
    SaaS系列介绍之十二: SaaS产品的研发模式
    个人总结
    用例图设计
    第二次结对作业
  • 原文地址:https://www.cnblogs.com/caizhiqin/p/9438876.html
Copyright © 2020-2023  润新知