1. 程式人生 > >ListUtils 對 list數據 分組 ,統計,求和 。。。

ListUtils 對 list數據 分組 ,統計,求和 。。。

override bject erro test collect ring per 返回 style

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() ); }

ListUtils 對 list數據 分組 ,統計,求和 。。。