集合操作工具类

package com.hungteshun.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

/**
 * @author hungteshun
 * @description:
 * @date 2018/12/12 19:48
 */
public class CollectionUtil {

    /**
     * 判断集合对象是否为空
     *
     * @param collection
     * @return
     */
    public static Boolean isEmpty(Collection<?> collection) {
        return (collection == null) || collection.isEmpty();
    }

    /**
     * 判断集合对象是否不为空
     *
     * @param collection
     * @return
     */
    public static Boolean isNotEmpty(Collection<?> collection) {
        return (collection != null) && !collection.isEmpty();
    }

    /**
     * 在collection中,得到propertyName属性去重后的属性集合
     *
     * @param collection
     * @param propertyName
     * @param <E>
     * @return
     */
    public static <E> Set<E> extractToSet(final Collection collection, final String propertyName) {
        Set set = Sets.newHashSet();
        if (isEmpty(collection)) {
            return set;
        }
        try {
            for (Object object : collection) {
                set.add(PropertyUtils.getProperty(object, propertyName));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return set;
    }

    /**
     * 从collection中抽取每个对象的propertyName属性值,放入list中
     *
     * @param collection
     * @param propertyName
     * @param <E>
     * @return
     */
    public static <E> List<E> extractToList(final Collection collection, final String propertyName) {
        List<E> list = Lists.newArrayList();
        if (isEmpty(collection)) {
            return list;
        }
        try {
            for (Object object : collection) {
                if (object != null) {
                    E value = (E) PropertyUtils.getProperty(object, propertyName);
                    if (value != null) {
                        list.add(value);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 从collection中以每个对象的keyPropertyName为key值,对象为value放置在map中
     *
     * @param collection
     * @param keyPropertyName
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> extractToMap(final Collection collection, final String keyPropertyName) {
        Map<K, V> map = Maps.newHashMap();
        if (isEmpty(collection)) {
            return map;
        }
        try {
            for (Object object : collection) {
                if (object != null) {
                    K key = (K) PropertyUtils.getProperty(object, keyPropertyName);
                    if (key != null) {
                        map.put(key, (V) object);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static <K, E> Map<K, List<E>> extractToMapList(final Collection collection, final String keyPropertyName) {
        Map<K, List<E>> map = Maps.newHashMap();
        if (isEmpty(collection)) {
            return map;
        }
        try {
            for (Object object : collection) {
                if (object != null) {
                    K key = (K) PropertyUtils.getProperty(object, keyPropertyName);
                    if (key != null) {
                        if (!map.containsKey(key)) {
                            map.put(key, Lists.<E>newArrayList());
                        }
                        map.get(key).add((E) object);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 两个集合组合成一个list
     *
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> List<T> union(final Collection<T> a, final Collection<T> b) {
        List<T> result = new ArrayList<T>(a);
        result.addAll(b);
        return result;
    }

    /**
     * 从集合a中删除存在于集合b中的元素
     *
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) {
        List<T> result = new ArrayList<T>(a);
        for (T element : b) {
            result.remove(element);
        }
        return result;
    }

    /**
     * 返回a与b的交集的新List
     *
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> List<T> interSection(final Collection<T> a, final Collection<T> b) {
        List<T> result = new ArrayList<T>();
        for (T element : a) {
            if (b.contains(element)) {
                result.add(element);
            }
        }
        return result;
    }

    /**
     * 集合a与集合b去除了相同元素的集合
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> List<T> unionDiff(final Collection<T> a, final Collection<T> b) {
        List<T> subtractList = subtract(a, b);
        subtractList.addAll(b);
        return subtractList;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值