ListUtils 对 list数据 分组 ,统计,求和 。。。

listUtil.java

package com.icil.report.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * *************************************************************************
 * <PRE>
 *  @ClassName:    : ListUtil 
 *
 *  @Description:    : 
 *
 *  @Creation Date   : 13 May 2019 2:48:52 PM
 *
 *  @Author          :  Sea
 *  
 *
 * </PRE>
 **************************************************************************
 */
public class ListUtil{
    
    private static Logger  LOGGER=LoggerFactory.getLogger(ListUtil.class);
/**
     * 分组依据接口,用于集合分组时,获取分组
     * T为要groupBy属性是类型,这个返回值为要groupBy的属性值
     */
    public interface GroupBy<T> {
        T groupBy(Object obj) ;
    }
    
    
    
/**
     * 通过属性对集合分组
     * @param colls
     * @param gb
     * @return
     * extends Comparable<T> 
     */
public static final <T,D> Map<T ,List<D>> groupBy(Collection<D> colls ,GroupBy<T> gb){
        Map<T ,List<D>> map = new HashMap<T, List<D>>();
        
        Iterator<D> iter = colls.iterator() ;
        
        while(iter.hasNext()) {
            D d = iter.next() ;
            T t = gb.groupBy(d) ;
            if(map.containsKey(t)) {
                map.get(t).add(d) ;
            } else {
                List<D> list = new ArrayList<D>() ;
                list.add(d) ;
                map.put(t, list) ;
            }
        }
        return map ;
    }


    
     /**
     * 通过属性名称对集合分组
     * @param colls
     * @param fieldName为集合中对象的属性名称
     * @return
     * extends Comparable<T> 
     */
    public static final <T,D> Map<T ,List<D>> groupBy(Collection<D> colls ,String fieldName){
        return groupBy(colls,new GroupBy<T>(){
            @Override
            public T groupBy(Object obj){
                Object v=getFieldValueByName(obj,fieldName);
                return (T)v;
            }
        });
    }
    
    
    
    /** 
     * 根据属性名称获取属性值 
     * */  
   public static Object getFieldValueByName(Object o,String fieldName) {  
       try {    
           String firstLetter = fieldName.substring(0, 1).toUpperCase();    
           String getter = "get" + firstLetter + fieldName.substring(1);    
           Method method = o.getClass().getMethod(getter, new Class[] {});    
           Object value = method.invoke(o, new Object[] {});    
           return value;    
       } catch (Exception e) {    
           LOGGER.error(e.getMessage(),e);    
           return null;    
       }    
   }
   
   
   
   /** 
    * 根据属性名称获取属性值 (获取long型)
    * */  
  public static Long getFieldLongValueByName(Object o,String fieldName) {  
      try {    
          String firstLetter = fieldName.substring(0, 1).toUpperCase();    
          String getter = "get" + firstLetter + fieldName.substring(1);    
          Method method = o.getClass().getMethod(getter, new Class[] {});    
          Long value = (Long) method.invoke(o, new Object[] {});    
          return value;    
      } catch (Exception e) {    
          LOGGER.error(e.getMessage(),e);    
          return null;    
      }    
  }
   
   
   
   
   
   
   /**
    * 通过属性名称对集合分组
    * @Desc:java 8 stream api, list 会过滤掉group by filed 的非空字段
    * @param colls   集合必须为对象 eg: List<Employee>
    * @param fieldName为集合中对象的属性名称  eg: Employee-->name
    * @return
    * extends Comparable<T> 
    */
   public static final <D> Map<Object ,List<D>> groupByPro(Collection<D> colls ,String fieldName){
       
           //filter
        List<D> filterlist = colls.parallelStream().filter(r->getFieldValueByName(r,fieldName)!=null).collect(Collectors.toList());
        //group by 
        Map<Object, List<D>> collect = filterlist.stream().collect(Collectors.groupingBy(r->getFieldValueByName(r,fieldName)));
        return collect;
   }
   
   
   /**
    * 通过属性名称对集合分组统计
    * @Desc:java 8 stream api, list 会过滤掉group by filed 的非空字段
    * @param colls   集合必须为对象 eg: List<Employee>
    * @param fieldName为集合中对象的属性名称  eg: Employee-->name
    * @return
    * extends Comparable<T> 
    */
   public static final <D> Map<Object, Long>  groupByAndCount(Collection<D> colls ,String fieldName){
       
        //filter
         List<D> filterlist = colls.parallelStream().filter(r->getFieldValueByName(r,fieldName)!=null).collect(Collectors.toList());
         //group by 
         Map<Object, Long> collect = filterlist.parallelStream().collect(Collectors.groupingBy(r->getFieldValueByName(r,fieldName),Collectors.counting()));
        
        return collect;
   }
   
   

   /**
    * 通过属性名称对集合分组统计求和
    * @Desc:java 8 stream api, list 会过滤掉group by filed 的非空字段
    * @param colls   eg: List<Employee>
    * @param groupByFieldName  根据那个字段分组
    * @param sumFieldName      对那个字段进行求和
    * @return
    *  extends Comparable<T> 
    */
   public static final <D>  Map<Object, Long>  groupByAndSum(Collection<D> colls ,String groupByFieldName,String sumFieldName){
       
        //filter
         List<D> filterlist = colls.parallelStream().filter(r->getFieldValueByName(r,groupByFieldName)!=null).collect(Collectors.toList());
         //group by  and sum
         
         Map<Object, Long> collect = filterlist.stream().collect(
                 Collectors.groupingBy(r->getFieldValueByName(r,groupByFieldName), Collectors.summingLong(r->getFieldLongValueByName(r,sumFieldName))));
        return collect;
   }
   
 
}

test:

List<ReportDetailInfo> list = ediPdJdbcTemplate.query(sql,new BeanPropertyRowMapper(ReportDetailInfo.class)) ;
Map
<Object, List<ReportDetailInfo>> groupByPro = ListUtil.groupByPro(list, "courier");
Set
<Entry<Object, List<ReportDetailInfo>>> entrySet = groupByPro.entrySet(); for (Entry<Object, List<ReportDetailInfo>> entry : entrySet) { System.err.println(entry.getKey()+"---> size"+entry.getValue() ); }

猜你喜欢

转载自www.cnblogs.com/lshan/p/10861629.html