JDK 1.8 Sream 分组的使用


public class Apple {

  /**
   * 主键
   */
  private String id;

  /**
   * 名称
   */
  private String name;

  /**
   * 价格
   */
  private BigDecimal price;

  /**
   * 总数
   */
  private Long count;

  /**
   * 类别
   */
  private String type;

  public Apple() {

  }

  public Apple(String id, String name, BigDecimal price, Long count) {
    this.id = id;
    this.name = name;
    this.price = price;
    this.count = count;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public BigDecimal getPrice() {
    return price;
  }

  public void setPrice(BigDecimal price) {
    this.price = price;
  }

  public Long getCount() {
    return count;
  }

  public void setCount(Long count) {
    this.count = count;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public static void main(String[] args) {

    List<Apple> appleList = Lists.newArrayList();
    List<Map<String, Object>> mapList = Lists.newArrayList();
    Apple apple1 = new Apple();
    Map<String, Object> map11 = new HashMap<>();
    map11.put("ID", "111111");
    map11.put("Name", "fendo1");
    map11.put("count", (long) 20);
    map11.put("type", "1");
    map11.put("price", new BigDecimal(20));
    mapList.add(map11);
    apple1.setId("111111");
    apple1.setName("fendo1");
    apple1.setCount((long) 10);
    apple1.setType("1");
    apple1.setPrice(new BigDecimal(20));
    appleList.add(apple1);

    Apple apple2 = new Apple();
    Map<String, Object> map1 = new HashMap<>();
    map1.put("ID", "33333322");
    map1.put("Name", "fendo");
    map1.put("count", (long) 10);
    map1.put("type", "1");
    map1.put("price", new BigDecimal(20));
    mapList.add(map1);
    apple2.setId("222222222");
    apple2.setName("fendo2");
    apple2.setCount((long) 10);
    apple2.setType("1");
    apple2.setPrice(new BigDecimal(20));
    appleList.add(apple2);

    Apple apple6 = new Apple();
    Map<String, Object> map2 = new HashMap<>();
    map2.put("ID", "33333333");
    map2.put("Name", "fendo");
    map2.put("count", (long) 10);
    map2.put("type", "1");
    map2.put("price", new BigDecimal(20));
    mapList.add(map2);
    apple6.setId("64444444");
    apple6.setName("fendo");
    apple6.setCount((long) 30);
    apple6.setType("1");
    apple6.setPrice(new BigDecimal(20));
    appleList.add(apple6);

    Apple apple3 = new Apple();
    Map<String, Object> map3 = new HashMap<>();
    map3.put("ID", "4444444");
    map3.put("Name", "fendo1");
    map3.put("count", (long) 10);
    map3.put("type", "2");
    map3.put("price", new BigDecimal(20));
    mapList.add(map3);
    apple3.setId("3444444444");
    apple3.setName("fendo");
    apple3.setCount((long) 10);
    apple3.setType("2");
    apple3.setPrice(new BigDecimal(20));
    appleList.add(apple3);

    Apple apple4 = new Apple();
    Map<String, Object> map4 = new HashMap<>();
    map4.put("ID", "55555555");
    map4.put("Name", "fendo4");
    map4.put("count", (long) 10);
    map4.put("type", "3");
    map4.put("price", new BigDecimal(20));
    mapList.add(map4);
    apple4.setId("45555555");
    apple4.setName("fendo");
    apple4.setCount((long) 10);
    apple4.setType("3");
    apple4.setPrice(new BigDecimal(20));
    appleList.add(apple4);

    Apple apple5 = new Apple();
    Map<String, Object> map5 = new HashMap<>();
    map5.put("ID", "6666666");
    map5.put("Name", "fendo");
    map5.put("count", (long) 10);
    map5.put("type", "3");
    map5.put("price", new BigDecimal(20));
    mapList.add(map5);
    apple5.setId("56666666");
    apple5.setName("fendo5");
    apple5.setCount((long) 10);
    apple5.setType("4");
    apple5.setPrice(new BigDecimal(20));
    appleList.add(apple5);

    //分组
    Map<String, List<Apple>> map = appleList.stream().collect(Collectors.groupingBy(Apple::getType));
    for (Map.Entry<String, List<Apple>> entry : map.entrySet()) {
      System.out.println("分组:" + entry);
    }

    //分组求和
    Map<String, LongSummaryStatistics> collect = appleList.stream()
      .collect(Collectors.groupingBy(Apple::getType, Collectors.summarizingLong(Apple::getCount)));
    for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
      LongSummaryStatistics longSummaryStatistics = entry.getValue();
      System.out.println("----------------key----------------" + entry.getKey());
      System.out.println("求和:" + longSummaryStatistics.getSum());
      System.out.println("求平均" + longSummaryStatistics.getAverage());
      System.out.println("求最大:" + longSummaryStatistics.getMax());
      System.out.println("求最小:" + longSummaryStatistics.getMin());
      System.out.println("求总数:" + longSummaryStatistics.getCount());
    }
    Objects.requireNonNull(appleList, "The parameter cannot be empty");
    Map<String, List<Apple>> qqq = appleList.stream()
      .collect(Collectors.groupingBy(Apple::getType, Collectors.toList()));

    Map<Object, List<Map<String, Object>>> qqqq1 = mapList.stream().collect(Collectors.groupingBy(Map -> {
      return Map.get("type");
    }, Collectors.toList()));

    //    List<Map<String, Object>> aaa = qqqq.get("1");
    for (Entry<Object, List<Map<String, Object>>> aaa : qqqq1.entrySet()) {
      List<Map<String, Object>> llll = aaa.getValue();
      Map<Object, LongSummaryStatistics> sss = llll.stream().collect(Collectors.groupingBy(Map -> {
        return Map.get("type");
      }, Collectors.summarizingLong(Map -> {
        return (long) Map.get("count");
      })));

      System.out.println(sss);
    }

    System.out.println(qqq);

    //----------------------------------对字段分组汇总
    User user1 = new User("zhangsan", "beijing", 10);
    User user2 = new User("zhangsan", "beijing", 20);
    User user3 = new User("lisi", "shanghai", 30);
    User user5 = new User("lisi", "shanghai", 30);
    User user4 = new User("zhangsan", "shanghai", 30);
    List<User> list = new ArrayList<User>();
    list.add(user1);
    list.add(user2);
    list.add(user3);
    list.add(user4);
    list.add(user5);
    Map<String, List<User>> collect1 = list.stream().collect(Collectors.groupingBy(e -> fetchGroupKey(e)));
    //{zhangsan#beijing=[User{age=10, name='zhangsan', address='beijing'}, User{age=20, name='zhangsan', address='beijing'}], 
    // lisi#shanghai=[User{age=30, name='lisi', address='shanghai'}]}
    System.out.println(collect);

    Objects.requireNonNull(mapList, "The parameter cannot be empty");
    Map<Object, List<Map<String, Object>>> qqqq = mapList.stream().collect(Collectors.groupingBy(Map -> {
      List<Object> aq = Lists.newArrayList();
      aq.add(Map.get("type"));
      aq.add(Map.get("Name"));
      return aq;
    }, Collectors.toList()));

  }

  private static String fetchGroupKey(User user) {
    return user.getName() + "#" + user.getAddress();
  }
}
public class User {
  private String name;

  private String address;

  private int age;

  public User(String name, String address, int age) {
    this.name = name;
    this.address = address;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

}

猜你喜欢

转载自blog.csdn.net/limingdepoxiao/article/details/89668803