集合操作工具类(list、map)

好用的集合操作工具类,直接导入到utils包下
具体应用:
getSet
List zbIds =MyCollectionUtil.getList(list, “id”);
Map<String, List> ListMap =MyCollectionUtil.aggregate(MxList, “zbid”);

/**
 * 功能: 集合工具类
 * 从fcgoods 应用中拷贝
 * @author yanguofu
 * @version 1.0
 */
public class MyCollectionUtil {
    /**
     * 功能: 遍历list,根据function业务逻辑进行聚合分组,返回Map key=function处理的值  value=List<T>
     * @author yanguofu
     * @param <T> 泛型对象
     * @param <FiledType> 字段对应的数据类型
     * @param list 列表结合
     * @param function 功能函数
     * @return Map<FiledType,List<T>> FiledType 字段类型
     */
    public static <T, FiledType> Map<FiledType, List<T>> aggregate(List<T> list, Function<T, FiledType> function) {
        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
        }
        Map<FiledType, List<T>> map = new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
        List<T> tempList = null;
        for (T t : list) {
            FiledType fieldValue;
            try {
                fieldValue = function.apply(t);
                tempList = map.get(fieldValue);
                if (null == tempList) {
                    tempList = new ArrayList<T>();
                    tempList.add(t);
                    map.put(fieldValue, tempList);
                } else {
                    tempList.add(t);
                }

            } catch (Exception e) {
            }

        }
        return map;
    }
    
    /**
     * 功能: 遍历list,根据fieldName属性进行聚合分组,返回Map key=fieldName属性值  value=List<T>
     * @author yanguofu
     * @param <T> 泛型对象
     * @param <FiledType> 字段对应的数据类型
     * @param list 列表集合
     * @param fieldName 属性名称
     * @return Map<FiledType,List<T>> Map集合结果
     */
    public static <T, FiledType> Map<FiledType, List<T>> aggregate(List<T> list, String fieldName) {
        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
        }
        Map<FiledType, List<T>> map = new HashMap<FiledType, List<T>>(MagicNumberConstant.INT16);
        List<T> tempList = null;
        for (T t : list) {
            FiledType fieldValue;
            try {
                fieldValue = MyCollectionUtil.getProperty(t, fieldName);
                tempList = map.get(fieldValue);
                if (null == tempList) {
                    tempList = new ArrayList<T>();
                    tempList.add(t);
                    map.put(fieldValue, tempList);
                } else {
                    tempList.add(t);
                }

            } catch (Exception e) {
            }
        }
        return map;
    }
    
     /**
     * 从list提取字段根据function业务逻辑,生成一个List集合,并返回
     * @param <T> 泛型对象
     * @param <FiledType> 字段对应的数据类型
     * @param list 列表集合
     * @param fieldName 属性名称
     * @return List<FiledType> 属性值集合
     */
    public static <T, FiledType> List<FiledType> getList(List<T> list, String fieldName) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<FiledType> fieldList = new ArrayList<>();
        for (T t : list) {
            FiledType fieldValue;
            try {
                fieldValue = MyCollectionUtil.getProperty(t, fieldName);
                if (null != fieldValue) {
                    fieldList.add(fieldValue);
                }

            } catch (Exception e) {
            }

        }
        return fieldList;
    }
    
    /**
     * 遍历list,根据fieldName属性进行聚合分组,每个分组只有一个对象,返回Map key=fieldName属性值  value= T
     * @param <T> 泛型对象
     * @param <FiledType> 字段对应的数据类型
     * @param list 列表集合
     * @param fieldName 属性名称
     * @return Map<FiledType, T> 属性值集合
     */
    public static <T, FiledType> Map<FiledType, T> aggregateForOne(List<T> list, String fieldName) {
        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<>(MagicNumberConstant.INT16);
        }
        Map<FiledType, T> map = new HashMap<FiledType, T>(MagicNumberConstant.INT16);
        T tempObject = null;
        for (T t : list) {
            FiledType fieldValue;
            try {
                fieldValue = MyCollectionUtil.getProperty(t, fieldName);
                tempObject = map.get(fieldValue);
                if (tempObject != null) {
                    map.put(fieldValue, tempObject);
                } else {
                    map.put(fieldValue, t);
                }
            } catch (Exception e) {
            }

        }
        return map;
    }
    
    /**
     * 功能: 获取对象属性值
     * @author yanguofu
     * @param <T> 泛型对象
     * @param beanObject 对象
     * @param fieldName 属性名称
     * @return T 对象类型
     */
    public static <T> T getProperty(Object beanObject, String fieldName) {
        T fieldValue = null;
        try {
            fieldValue = (T) BeanUtilsBean.getInstance().getPropertyUtils().getNestedProperty(beanObject, fieldName);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }

        return fieldValue;
    }
    
     /**
     * 从list提取字段fieldName字段中的值生成一个Set集合,并返回
     *
     * @param list
     * @param fieldName
     * @return Set<FiledType>
     */
    public static <T, FiledType> Set<FiledType> getSet(List<T> list, String fieldName) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        Set<FiledType> set = new HashSet<FiledType>();
        for (T t : list) {
            FiledType fieldValue;
            try {
                fieldValue = MyCollectionUtil.getProperty(t, fieldName);
                set.add(fieldValue);
            } catch (Exception e) {
                
            }

        }
        return set;
    }
    
     /**
     * 从list提取字段根据function业务逻辑,生成一个Set集合,并返回
     *
     * @param list
     * @param function
     * @return Set<E>
     */
    public static <T, E> Set<E> getSet(List<T> list, Function<T, E> function) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        Set<E> set = new HashSet<E>();
        for (T t : list) {
            try {
                set.add(function.apply(t));
            } catch (Exception e) {
                
            }

        }
        return set;
    }
}        
                
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值