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;
}
}
集合操作工具类
最新推荐文章于 2022-02-18 21:32:17 发布