三叔博客 - lambda
type: channel
link: https://www.sanshu.cn/tag/lambda/
items: Java集合类lambda工具类

Java集合类lambda工具类
type: item
date: 2025-01-10T09:44:27+08:00
description: package cn.jk.cloud.framework.common.util.collection; import cn.hutool.core.collection.CollUtil;import cn.hutool.core.collection.CollectionUtil;import com.google.common.collect.ImmutableMap;import java.util.*;import java.util.function.BinaryOperator;import java.util.function.Function;import java.util.function.Predicate;import java.util.function.Supplier;import java.util.stream.Collectors;/**Collection 工具类 *@author 芋道源码 */public class CollectionUtils {public static boolean containsAny(Object source, Object... targets) { return Arrays.asList(targets).contains(source); } public static boolean isAnyEmpty(Collection<?>... collections) { return Arrays.stream(collections).anyMatch(CollectionUtil::isEmpty); } public static <T> List<T> filterList(Collection<T> from, Predicate<T> predicate) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().filter(predicate).collect(Collectors.toList()); } public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return distinct(from, keyMapper, (t1, t2) -> t1); } public static <T, R> List<T> distinct(Collection<T> from, Function<T, R> keyMapper, BinaryOperator<T> cover) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return new ArrayList<>(convertMap(from, keyMapper, Function.identity(), cover).values()); } public static <T, U> List convertList(Collection<T> from, Function<T, U> func) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().map(func).filter(Objects::nonNull).collect(Collectors.toList()); } public static <T, U> List convertList(Collection<T> from, Function<T, U> func, Predicate<T> filter) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().filter(filter).map(func).filter(Objects::nonNull).collect(Collectors.toList()); } public static <T, U> Set convertSet(Collection<T> from, Function<T, U> func) { if (CollUtil.isEmpty(from)) { return new HashSet<>(…