常用工具类
org.apache.commons commons-collections4 4.4
import org.apache.commons.collections4.CollectionUtils;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
/**
* 集合工具类
*/
public class CollUtils {
private CollUtils() {
throw new IllegalStateException("Utility class");
}
public static ArrayList emptyArrayList() {
return new ArrayList<>();
}
public static HashMap emptyHashMap() {
return new HashMap<>();
}
public static LinkedHashMap emptyLinkedHashMap() {
return new LinkedHashMap<>();
}
public static HashSet emptyHashSet() {
return new HashSet<>();
}
/**
* 判断集合是否是为空
*
* @param map
* @return
*/
public static boolean isEmpty(Map map) {
return (map == null || map.isEmpty());
}
/**
* 判断集合是否不为空
*
* @param map
* @return
*/
public static boolean isNotEmpty(Map map) {
return !isEmpty(map);
}
/**
* 判断集合是否是为空
*
* @param coll
* @return
*/
public static boolean isEmpty(Collection> coll) {
return (coll == null || coll.isEmpty());
}
/**
* 判断集合是否不为空
*
* @param coll
* @return
*/
public static boolean isNotEmpty(Collection> coll) {
return !isEmpty(coll);
}
/**
* 判断集合是否不为空
*
* @param coll
* @return
*/
public static T isNotEmpty(Collection> coll, Supplier supplier) {
if (isNotEmpty(coll)) {
return supplier.get();
}
return null;
}
/**
* 去重
*
* @param list
* @param
* @return
*/
public static Collection distinct(Collection list) {
return isEmpty(list) ? list : distinct(list, item -> item);
}
/**
* 去重
*
* @param from
* @param func
* @param
* @param
* @return
*/
public static List distinct(Collection from, Function func) {
return distinct(from, func, t -> true);
}
/**
* 去重
*
* @param from
* @param func
* @param filter
* @param
* @param
* @return
*/
public static List distinct(Collection from, Function func, Predicate filter) {
if (isEmpty(from)) {
return new ArrayList<>();
}
return from.stream().filter(filter).map(func).distinct().collect(Collectors.toList());
}
/**
* 将一个集合转换为另外一个集合
*
* @param from
* @param func
* @param
* @param
* @return
*/
public static List convertList(Collection from, Function func) {
if (isEmpty(from)) {
return new ArrayList<>();
}
return from.stream().map(func).collect(Collectors.toList());
}
/**
* 将一个集合转换为另外一个集合:from->filter->list
*
* @param from
* @param func
* @param filter
* @param
* @param
* @return
*/
public static List convertList(Collection from, Function func, Predicate filter) {
if (isEmpty(from)) {
return new ArrayList<>();
}
return from.stream().filter(filter).map(func).collect(Collectors.toList());
}
/**
* 将集合转换为set
*
* @param from
* @param func
* @param
* @param
* @return
*/
public static Set convertSet(Collection from, Function func) {
if (isEmpty(from)) {
return new HashSet<>();
}
return from.stream().map(func).collect(Collectors.toSet());
}
/**
* 将集合转换为set:from->filter->list
*
* @param from
* @param func
* @param filter
* @param
* @param
* @return
*/
public static Set convertSet(Collection from, Function func, Predicate filter) {
if (isEmpty(from)) {
return new HashSet<>();
}
return from.stream().filter(filter).map(func).collect(Collectors.toSet());
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc
* @param
* @param
* @return
*/
public static Map convertMap(Collection from, Function keyFunc) {
if (isEmpty(from)) {
return new HashMap<>();
}
return convertMap(from, keyFunc, Function.identity());
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc
* @param supplier
* @param
* @param
* @return
*/
public static Map convertMap(Collection from, Function keyFunc, Supplier extends Map> supplier) {
if (isEmpty(from)) {
return supplier.get();
}
return convertMap(from, keyFunc, Function.identity(), supplier);
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc
* @param valueFunc
* @param
* @param
* @param
* @return
*/
public static Map convertMap(Collection from, Function keyFunc, Function valueFunc) {
if (isEmpty(from)) {
return new HashMap<>();
}
return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1);
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc key转换器
* @param valueFunc value转换器
* @param mergeFunction key重复时value处理策略
* @param
* @param
* @param
* @return
*/
public static Map convertMap(Collection from, Function keyFunc, Function valueFunc, BinaryOperator mergeFunction) {
if (isEmpty(from)) {
return new HashMap<>();
}
return convertMap(from, keyFunc, valueFunc, mergeFunction, HashMap::new);
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc
* @param valueFunc
* @param supplier
* @param
* @param
* @param
* @return
*/
public static Map convertMap(Collection from, Function keyFunc, Function valueFunc, Supplier extends Map> supplier) {
if (isEmpty(from)) {
return supplier.get();
}
return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1, supplier);
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc
* @param valueFunc
* @param mergeFunction
* @param supplier
* @param
* @param
* @param
* @return
*/
public static Map convertMap(Collection from, Function keyFunc, Function valueFunc, BinaryOperator mergeFunction, Supplier extends Map> supplier) {
if (isEmpty(from)) {
return new HashMap<>();
}
return from.stream().collect(Collectors.toMap(keyFunc, valueFunc, mergeFunction, supplier));
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc
* @param
* @param
* @return
*/
public static Map> convertMultiMap(Collection from, Function keyFunc) {
if (isEmpty(from)) {
return new HashMap<>();
}
return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(t -> t, Collectors.toList())));
}
/**
* 将集合转换为map
*
* @param from
* @param keyFunc
* @param
* @param
* @return
*/
public static Map> convertMultiMap(Collection from, Function keyFunc, Function valueFunc) {
if (isEmpty(from)) {
return new HashMap<>();
}
return from.stream()
.collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toList())));
}
/**
* 创建 ArrayList
*
* @param args
* @return
*/
public static List newArrayList(E... args) {
return new ArrayList(Arrays.asList(args));
}
/**
* 创建 ArrayList
*
* @param initialCapacity
* @return
*/
public static List newArrayListCapacity(int initialCapacity) {
return new ArrayList(initialCapacity);
}
/**
* 创建HashSet
*
* @param args
* @return
*/
public static Set newHashSet(E... args) {
return new HashSet<>(Arrays.asList(args));
}
/**
* 创建LinkedHashSet
*
* @param args
* @return
*/
public static Set newLinkedHashSet(E... args) {
return new LinkedHashSet<>(Arrays.asList(args));
}
/**
* 创建hashMap
*
* @param args
* @return
*/
@SuppressWarnings("rawtypes")
public static Map newHashMap(Object... args) {
HashMap paramMap = new HashMap();
if (args != null) {
if (args.length % 2 == 0) {
throw new RuntimeException("The length must be a multiple of 2");
}
int size = args.length / 2;
for (int i = 0; i < size; i++) {
paramMap.put(args[2 * i], args[2 * i + 1]);
}
}
return paramMap;
}
/**
* 创建LinkedHashMap
*
* @param args
* @return
*/
public static LinkedHashMap newLinkedHashMap(Object... args) {
LinkedHashMap paramMap = new LinkedHashMap();
if (args != null) {
if (args.length % 2 == 0) {
throw new RuntimeException("The length must be a multiple of 2");
}
int size = args.length / 2;
for (int i = 0; i < size; i++) {
paramMap.put(args[2 * i], args[2 * i + 1]);
}
}
return paramMap;
}
/**
* 都不为空返回true
*
* @param values
* @return
*/
public static boolean allNotEmpty(final Collection>... values) {
if (values == null) {
return false;
}
for (Collection val : values) {
if (isEmpty(val)) {
return false;
}
}
return true;
}
/**
* 都为空,返回true
*
* @param values
* @return
*/
public static boolean allEmpty(final Collection>... values) {
return !anyNotEmpty(values);
}
/**
* 任意一个不为空,则返回true
*
* @param values
* @return
*/
public static boolean anyNotEmpty(final Collection>... values) {
return firstNotEmpty(values) != null;
}
/**
* 任意一个为空,则返回true
*
* @param values
* @return
*/
public static boolean anyEmpty(final Collection>... values) {
return !allNotEmpty(values);
}
/**
* 返回第一个不为空的集合
*
* @param values
* @return
*/
public static Collection> firstNotEmpty(final Collection>... values) {
if (values != null) {
for (final Collection val : values) {
if (isNotEmpty(val)) {
return val;
}
}
}
return null;
}
/**
* 返回第一个元素
*
* @param list
* @param
* @return
*/
public static T getFirst(Collection list) {
return isNotEmpty(list) ? list.iterator().next() : null;
}
/**
* 返回第一个元素的某个属性
*
* @param list
* @param
* @return
*/
public static R getFirst(Collection list, Function fun) {
T first = getFirst(list);
return first == null ? null : fun.apply(first);
}
/**
* 返回第一个元素的某个属性
*
* @param list
* @param
* @return
*/
public static void first(Collection list, Consumer consumer) {
T first = getFirst(list);
if (null != first) {
consumer.accept(first);
}
}
/**
* 根据 key 获取值
*
* @param map
* @param key
* @param
* @param
* @return
*/
public static V get(Map map, K key) {
return map != null ? map.get(key) : null;
}
/**
* 转换为 Set
*
* @param list
* @param
* @return
*/
public static Set convertToSet(Collection list) {
if (list == null) {
return null;
}
if (Set.class.isInstance(list)) {
return (Set) list;
}
return list.stream().collect(Collectors.toSet());
}
/**
* 转换为 List
*
* @param list
* @param
* @return
*/
public static List convertToList(Collection list) {
if (list == null) {
return null;
}
if (List.class.isInstance(list)) {
return (List) list;
}
return list.stream().collect(Collectors.toList());
}
/**
* list 中是否包含 item
*
* @param list
* @param item
* @param
* @return
*/
public static boolean contain(Collection list, T item) {
return CollUtils.isNotEmpty(list) && list.contains(item);
}
/**
* 获取集合对象大小
*
* @param object
* @return
*/
public static int size(final Object object) {
return CollectionUtils.size(object);
}
/**
* 判断两个{@link Collection} 是否元素和顺序相同,返回{@code true}的条件是:
*
* - 两个{@link Collection}必须长度相同
* - 两个{@link Collection}元素相同index的对象必须equals,满足{@link Objects#equals(Object, Object)}
*
* 此方法来自Apache-Commons-Collections4。
*
* @param list1 列表1
* @param list2 列表2
* @return 是否相同
*/
public static boolean isEqualOrderList(final Collection> list1, final Collection> list2) {
if (list1 == null || list2 == null || list1.size() != list2.size()) {
return false;
}
final Iterator> it1 = list1.iterator();
final Iterator> it2 = list2.iterator();
Object obj1;
Object obj2;
while (it1.hasNext() && it2.hasNext()) {
obj1 = it1.next();
obj2 = it2.next();
if (false == Objects.equals(obj1, obj2)) {
return false;
}
}
// 当两个Iterable长度不一致时返回false
return false == (it1.hasNext() || it2.hasNext());
}
/**
* 判断两个集合的元素是否一样
*
* @param a
* @param b
* @param orderEqual 元素顺序是否也要一样?
* @return
*/
public static boolean isEqualCollection(final Collection> a, final Collection> b, boolean orderEqual) {
if (orderEqual) {
return isEqualOrderList(a, b);
}
return CollectionUtils.isEqualCollection(a, b);
}
}