package com.iailab.framework.common.util.collection; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ArrayUtil; import com.google.common.collect.ImmutableMap; import com.iailab.framework.common.pojo.PageResult; import java.util.*; import java.util.function.*; import java.util.stream.Collectors; import java.util.stream.Stream; import static java.util.Arrays.asList; /** * Collection 工具类 * * @author iailab */ public class CollectionUtils { public static boolean containsAny(Object source, Object... targets) { return asList(targets).contains(source); } public static boolean isAnyEmpty(Collection... collections) { return Arrays.stream(collections).anyMatch(CollectionUtil::isEmpty); } public static boolean anyMatch(Collection from, Predicate predicate) { return from.stream().anyMatch(predicate); } public static List filterList(Collection from, Predicate predicate) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().filter(predicate).collect(Collectors.toList()); } public static List distinct(Collection from, Function keyMapper) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return distinct(from, keyMapper, (t1, t2) -> t1); } public static List distinct(Collection from, Function keyMapper, BinaryOperator cover) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return new ArrayList<>(convertMap(from, keyMapper, Function.identity(), cover).values()); } public static List convertList(T[] from, Function func) { if (ArrayUtil.isEmpty(from)) { return new ArrayList<>(); } return convertList(Arrays.asList(from), func); } public static List convertList(Collection from, Function func) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().map(func).filter(Objects::nonNull).collect(Collectors.toList()); } public static List convertList(Collection from, Function func, Predicate filter) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().filter(filter).map(func).filter(Objects::nonNull).collect(Collectors.toList()); } public static PageResult convertPage(PageResult from, Function func) { if (ArrayUtil.isEmpty(from)) { return new PageResult<>(from.getTotal()); } return new PageResult<>(convertList(from.getList(), func), from.getTotal()); } public static List convertListByFlatMap(Collection from, Function> func) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().filter(Objects::nonNull).flatMap(func).filter(Objects::nonNull).collect(Collectors.toList()); } public static List convertListByFlatMap(Collection from, Function mapper, Function> func) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().map(mapper).filter(Objects::nonNull).flatMap(func).filter(Objects::nonNull).collect(Collectors.toList()); } public static List mergeValuesFromMap(Map> map) { return map.values() .stream() .flatMap(List::stream) .collect(Collectors.toList()); } public static Set convertSet(Collection from) { return convertSet(from, v -> v); } public static Set convertSet(Collection from, Function func) { if (CollUtil.isEmpty(from)) { return new HashSet<>(); } return from.stream().map(func).filter(Objects::nonNull).collect(Collectors.toSet()); } public static Set convertSet(Collection from, Function func, Predicate filter) { if (CollUtil.isEmpty(from)) { return new HashSet<>(); } return from.stream().filter(filter).map(func).filter(Objects::nonNull).collect(Collectors.toSet()); } public static Map convertMapByFilter(Collection from, Predicate filter, Function keyFunc) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return from.stream().filter(filter).collect(Collectors.toMap(keyFunc, v -> v)); } public static Set convertSetByFlatMap(Collection from, Function> func) { if (CollUtil.isEmpty(from)) { return new HashSet<>(); } return from.stream().filter(Objects::nonNull).flatMap(func).filter(Objects::nonNull).collect(Collectors.toSet()); } public static Set convertSetByFlatMap(Collection from, Function mapper, Function> func) { if (CollUtil.isEmpty(from)) { return new HashSet<>(); } return from.stream().map(mapper).filter(Objects::nonNull).flatMap(func).filter(Objects::nonNull).collect(Collectors.toSet()); } public static Map convertMap(Collection from, Function keyFunc) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return convertMap(from, keyFunc, Function.identity()); } public static Map convertMap(Collection from, Function keyFunc, Supplier> supplier) { if (CollUtil.isEmpty(from)) { return supplier.get(); } return convertMap(from, keyFunc, Function.identity(), supplier); } public static Map convertMap(Collection from, Function keyFunc, Function valueFunc) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1); } public static Map convertMap(Collection from, Function keyFunc, Function valueFunc, BinaryOperator mergeFunction) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return convertMap(from, keyFunc, valueFunc, mergeFunction, HashMap::new); } public static Map convertMap(Collection from, Function keyFunc, Function valueFunc, Supplier> supplier) { if (CollUtil.isEmpty(from)) { return supplier.get(); } return convertMap(from, keyFunc, valueFunc, (v1, v2) -> v1, supplier); } public static Map convertMap(Collection from, Function keyFunc, Function valueFunc, BinaryOperator mergeFunction, Supplier> supplier) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return from.stream().collect(Collectors.toMap(keyFunc, valueFunc, mergeFunction, supplier)); } public static Map> convertMultiMap(Collection from, Function keyFunc) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(t -> t, Collectors.toList()))); } public static Map> convertMultiMap(Collection from, Function keyFunc, Function valueFunc) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return from.stream() .collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toList()))); } // 暂时没想好名字,先以 2 结尾噶 public static Map> convertMultiMap2(Collection from, Function keyFunc, Function valueFunc) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); } return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toSet()))); } public static Map convertImmutableMap(Collection from, Function keyFunc) { if (CollUtil.isEmpty(from)) { return Collections.emptyMap(); } ImmutableMap.Builder builder = ImmutableMap.builder(); from.forEach(item -> builder.put(keyFunc.apply(item), item)); return builder.build(); } /** * 对比老、新两个列表,找出新增、修改、删除的数据 * * @param oldList 老列表 * @param newList 新列表 * @param sameFunc 对比函数,返回 true 表示相同,返回 false 表示不同 * 注意,same 是通过每个元素的“标识”,判断它们是不是同一个数据 * @return [新增列表、修改列表、删除列表] */ public static List> diffList(Collection oldList, Collection newList, BiFunction sameFunc) { List createList = new LinkedList<>(newList); // 默认都认为是新增的,后续会进行移除 List updateList = new ArrayList<>(); List deleteList = new ArrayList<>(); // 通过以 oldList 为主遍历,找出 updateList 和 deleteList for (T oldObj : oldList) { // 1. 寻找是否有匹配的 T foundObj = null; for (Iterator iterator = createList.iterator(); iterator.hasNext(); ) { T newObj = iterator.next(); // 1.1 不匹配,则直接跳过 if (!sameFunc.apply(oldObj, newObj)) { continue; } // 1.2 匹配,则移除,并结束寻找 iterator.remove(); foundObj = newObj; break; } // 2. 匹配添加到 updateList;不匹配则添加到 deleteList 中 if (foundObj != null) { updateList.add(foundObj); } else { deleteList.add(oldObj); } } return asList(createList, updateList, deleteList); } public static boolean containsAny(Collection source, Collection candidates) { return org.springframework.util.CollectionUtils.containsAny(source, candidates); } public static T getFirst(List from) { return !CollectionUtil.isEmpty(from) ? from.get(0) : null; } public static T findFirst(Collection from, Predicate predicate) { return findFirst(from, predicate, Function.identity()); } public static U findFirst(Collection from, Predicate predicate, Function func) { if (CollUtil.isEmpty(from)) { return null; } return from.stream().filter(predicate).findFirst().map(func).orElse(null); } public static > V getMaxValue(Collection from, Function valueFunc) { if (CollUtil.isEmpty(from)) { return null; } assert !from.isEmpty(); // 断言,避免告警 T t = from.stream().max(Comparator.comparing(valueFunc)).get(); return valueFunc.apply(t); } public static > V getMinValue(List from, Function valueFunc) { if (CollUtil.isEmpty(from)) { return null; } assert from.size() > 0; // 断言,避免告警 T t = from.stream().min(Comparator.comparing(valueFunc)).get(); return valueFunc.apply(t); } public static > T getMinObject(List from, Function valueFunc) { if (CollUtil.isEmpty(from)) { return null; } assert from.size() > 0; // 断言,避免告警 return from.stream().min(Comparator.comparing(valueFunc)).get(); } public static > V getSumValue(Collection from, Function valueFunc, BinaryOperator accumulator) { return getSumValue(from, valueFunc, accumulator, null); } public static > V getSumValue(Collection from, Function valueFunc, BinaryOperator accumulator, V defaultValue) { if (CollUtil.isEmpty(from)) { return defaultValue; } assert !from.isEmpty(); // 断言,避免告警 return from.stream().map(valueFunc).filter(Objects::nonNull).reduce(accumulator).orElse(defaultValue); } public static void addIfNotNull(Collection coll, T item) { if (item == null) { return; } coll.add(item); } public static Collection singleton(T obj) { return obj == null ? Collections.emptyList() : Collections.singleton(obj); } public static List newArrayList(List> list) { return list.stream().filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList()); } }