传统方式:ListUtil.java

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;    
       }    
   }}
View Code

 参考:https://blog.csdn.net/frankenjoy123/article/details/70739800

java stream:

ReportDetailInfo.java
@Data
@ToString
public class ReportDetailInfo {

     private String  account;
     
     private String  orderNo;
     
     private String  customer;
     
     private String  courier;
     
     private String  courierBillNo;
     
     private String  subscribedDate;
     
//     private String  bookingNO;
    
}

 

过滤:

List<ReportDetailInfo> list = ediPdJdbcTemplate.query(sql,new BeanPropertyRowMapper(ReportDetailInfo.class)) ;
        
        list.get(0).setCourier(null);
      
     //并发流 list
= list.parallelStream().filter(r->r.getCourier()!=null).collect(Collectors.toList());
     //串行流: list.stream.filter(r->r.gerCourier()!=null).collect(Collectors.toList());

分组:

Map<String, List<ReportDetailInfo>> collect = list.parallelStream().collect(Collectors.groupingBy(ReportDetailInfo::getCourier));

Set
<Entry<String, List<ReportDetailInfo>>> entrySet = collect.entrySet(); for (Entry<String, List<ReportDetailInfo>> entry : entrySet) { System.err.println(entry.getKey()+"---> size"+entry.getValue().size() ); }

 统计:

参数1:根据那个字段分组
参数2:数据统计
Map<String, Long> collect = list.parallelStream().collect(Collectors.groupingBy(ReportDetailInfo::getCourier,Collectors.counting()));
Set
<Entry<String, Long>> entrySet = collect.entrySet(); for (Entry<String, Long> entry : entrySet) { System.err.println(entry.getKey()+"---> size"+entry.getValue() ); }

 

posted on 2019-05-13 15:44  lshan  阅读(14881)  评论(0编辑  收藏  举报