Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

统计专家 AggregateUtil

feilong edited this page Nov 26, 2016 · 2 revisions

JAVA开发统计少不了的,我们来看看 统计专家 - AggregateUtil

主要由下面几个部分组成:

AggregateUtil

1.算平均.

方法 Description
avg(Collection, String, int) 算术平均值
avg(Collection, String[], int) 算术平均值

1.1 avg(Collection, String, int)

算术平均值.

示例: 场景: 求的User list 里面 id属性 的平均值

 List<User> list = new ArrayList<>();
 list.add(new User(2L));
 list.add(new User(5L));
 list.add(new User(5L));
 AggregateUtil.avg(list, "id", 2)

**返回:**4.00

1.2 avg(Collection, String[], int)

算术平均值.

说明:

  • 返回的 LinkedHashMap,key是 propertyNames的元素,value是基于这个属性名称获得的值的平均值;key的顺序是依照 propertyNames元素的顺序

示例: 场景: 求的User list 里面 age 以及id属性 的平均值

 User user1 = new User(2L);
 user1.setAge(18);
 
 User user2 = new User(3L);
 user2.setAge(30);
 
 List<User> list = toList(user1, user2);
 Map<String, BigDecimal> map = AggregateUtil.avg(list, ConvertUtil.toArray("id", "age"), 2);
 LOGGER.info(JsonUtil.format(map));

返回:

 {
 "id": 2.5,
 "age": 24
 }

2.分组求数量.

方法 Description
groupCount(Collection, String) 循环 objectCollection,统计 propertyName 的值出现的次数.
groupCount(Collection, String, Predicate) 循环 objectCollection,只选择符合 includePredicate的对象,统计 propertyName的值出现的次数.

2.1 groupCount(Collection, String)

循环 objectCollection,统计 propertyName 的值出现的次数.

说明:

  • 返回的LinkedHashMap,key是propertyName对应的值,value是该值出现的次数;
  • 顺序是 objectCollection propertyName的值的顺序

示例: 场景: 统计user list,属性名字是name 的值的数量

 List<User> list = new ArrayList<>();
 list.add(new User("张飞"));
 list.add(new User("关羽"));
 list.add(new User("刘备"));
 list.add(new User("刘备"));
 
 Map<String, Integer> map = AggregateUtil.groupCount(list, "name");
 LOGGER.info(JsonUtil.format(map));

返回:

 {
 "张飞": 1,
 "关羽": 1,
 "刘备": 2
 }

2.2 groupCount(Collection, String, Predicate)

循环 objectCollection,只选择符合 includePredicate的对象,统计 propertyName的值出现的次数.

说明:

  • 返回的LinkedHashMap,key是propertyName对应的值,value是该值出现的次数;
  • 顺序是 objectCollection propertyName的值的顺序

示例: 场景: 统计user list(条件是 age > 30 的user),name属性值的数量

 List<User> list = new ArrayList<>();
 list.add(new User("张飞", 20));
 list.add(new User("关羽", 30));
 list.add(new User("刘备", 40));
 list.add(new User("赵云", 50));
 
 Map<String, Integer> map = AggregateUtil.groupCount(list, "name", new Predicate<User>(){
     @Override
     public boolean evaluate(User user){
         return user.getAge() > 30;
     }
 });
 LOGGER.debug(JsonUtil.format(map));

返回:

 {
 "刘备": 1,
 "赵云": 1
 }

3.求和.

方法 Description
sum(Collection, String) 总和,计算集合对象objectCollection 内指定的属性名 propertyName 值的总和.
sum(Collection, String, Predicate) 迭代objectCollection,提取 符合 includePredicate的元素的指定 propertyName 元素的值 ,累计总和.
sum(Collection, String...) 总和,计算集合对象objectCollection 内指定的属性名 propertyNames 值的总和.
sum(Collection, String[], Predicate) 迭代objectCollection,提取符合 includePredicate的元素的指定 propertyNames 元素的值 ,累计总和.

3.1 sum(Collection, String)

总和,计算集合对象objectCollection 内指定的属性名 propertyName 值的总和.

说明:

  • 如果通过反射某个元素值是null,则使用默认值0代替,再进行累加

示例:

 List<User> list = new ArrayList<>();
 list.add(new User(2L));
 list.add(new User(5L));
 list.add(new User(5L));
 
 LOGGER.info("" + AggregateUtil.sum(list, "id"));

**返回:**12

说明: 当你需要写这样的代码的时候,

 protected Integer getCookieShoppingCartLinesQty(List<CookieShoppingCartLine> cartLineList){
     Integer qty = 0;
     //获取cookie中的购物车行集合
     if (null != cartLineList && cartLineList.size() > 0){
         for (Iterator iterator = cartLineList.iterator(); iterator.hasNext();){
             CookieShoppingCartLine cookieShoppingCartLine = (CookieShoppingCartLine) iterator.next();
             qty += cookieShoppingCartLine.getQuantity();
         }
     }
     return qty;
 }

你可以写成:

 protected Integer getCookieShoppingCartLinesQty(List<CookieShoppingCartLine> cartLineList){
     return isNullOrEmpty(cartLineList) ? 0 : AggregateUtil.sum(cartLineList, "quantity").intValue();
 }

3.2 sum(Collection, String, Predicate)

迭代objectCollection,提取符合 includePredicate的元素的指定 propertyName 元素的值 ,累计总和.

说明:

  • 如果通过反射某个元素值是null,则使用默认值0代替,再进行累加

示例:

场景: 统计user list(条件是 id >10),id属性值的总和

 List<User> list = new ArrayList<>();
 list.add(new User(2L));
 list.add(new User(50L));
 list.add(new User(50L));
 
 AggregateUtil.sum(list, "id", new Predicate<User>(){
 
     @Override
     public boolean evaluate(User user){
         return user.getId() > 10L;
     }
 });

返回:new BigDecimal(100L)

当然这段代码,你还可以优化成:

 Predicate<Long> predicate = new ComparatorPredicate<Long>(10L, ComparatorUtils.<Long> naturalComparator(), Criterion.LESS);
 BigDecimal sum = AggregateUtil.sum(list, "id", new BeanPredicate<User>("id", predicate));

3.3 sum(Collection, String...)

总和,计算集合对象objectCollection 内指定的属性名 propertyNames 值的总和.

说明:

  • 如果通过反射某个元素值是null,则使用默认值0代替,再进行累加

示例: 场景: 在user list 中,分别统计 id属性以及age属性值总和

 User user1 = new User(2L);
 user1.setAge(18);
 
 User user2 = new User(3L);
 user2.setAge(30);
 
 Map<String, BigDecimal> map = AggregateUtil.sum(toList(user1, user2), "id", "age");
 LOGGER.info(JsonUtil.format(map));

返回:

 {
 "id": 5,
 "age": 48
 }

3.4 sum(Collection, String[], Predicate)

迭代objectCollection,提取符合 includePredicate的元素的指定 propertyNames 元素的值 ,累计总和.

示例:

 User user1 = new User(10L);
 user1.setName("刘备");
 user1.setAge(50);
 
 User user2 = new User(20L);
 user1.setName("关羽");
 user2.setAge(50);
 
 User user3 = new User(100L);
 user3.setName("张飞");
 user3.setAge(100);
 
 List<User> list = toList(user1, user2, user3);
 Map<String, BigDecimal> map = AggregateUtil.sum(list, ConvertUtil.toArray("id", "age"), new Predicate<User>(){
 
     @Override
     public boolean evaluate(User user){
         return !"张飞".equals(user.getName());
     }
 });
 LOGGER.debug(JsonUtil.format(map));

返回:

 {
 "id": 30,
 "age": 100
 }
Clone this wiki locally