Lambda表达式工具类-日常搬砖


众所周知,jdk8提供的Lambda表达式能帮我极大的精简代码,但是就在我们在实际使用过程中会有一些重复性的代码,比如对于stream来源的空判断处理,今天我就给大家整理了一些工具类,帮助大家更好的使用流
public final class EnhanceStream {

public static  List singletonList(T o) {
    return Collections.singletonList(o);
}

/**
 * 生成list
 * 
 * @param elements
 *            元素
 * @param 
 *            泛型
 * @return List
 */
@SafeVarargs
public static  List toList(T... elements) {
    List list = new ArrayList<>();
    if (Objects.nonNull(elements)) {
        for (T element : elements) {
            if (Objects.nonNull(element)) {
                list.add(element);
            }
        }
    }
    return list;
}

public static  void addAll(Collection collection, Collection add) {
    if (isNotEmpty(add)) {
        collection.addAll(add);
    }
}

/**
 * 两个Map进行合并
 *
 * @param map
 *            原始map
 * @param add
 *            要合并的map
 * @param 
 *            key泛型
 * @param 
 *            值泛型
 */
public static  void addAll(Map map, Map add) {
    if (isNotEmpty(add) && Objects.nonNull(map)) {
        for (Map.Entry kvEntry : add.entrySet()) {
            map.put(kvEntry.getKey(), kvEntry.getValue());
        }
    }
}

public static  List setToList(Set set) {
    if (EnhanceStream.isNotEmpty(set)) {
        return new ArrayList<>(set);
    } else {
        return EnhanceStream.emptyList();
    }
}

public static boolean isEmpty(final Collection<?> coll) {
    return coll == null || coll.isEmpty();
}

/**
 * Null-safe check if the specified collection is not empty.
 * 

* Null returns false. * * @param coll * the collection to check, may be null * @return true if non-null and non-empty * @since 3.2 */ public static boolean isNotEmpty(final Collection<?> coll) { return !isEmpty(coll); } public static boolean isEmpty(final Map<?, ?> map) { return map == null || map.isEmpty(); } public static boolean isNotEmpty(final Map<?, ?> map) { return !isEmpty(map); } /** * 返回空集合 * * @param * 泛型 * @return List */ public static List emptyList() { return Collections.emptyList(); } public static final Map emptyMap() { return Collections.emptyMap(); } /** * 返回空集合 * * @param * 泛型 * @return List */ public static Set emptySet() { return Collections.emptySet(); } /** * 去重 * * @param dataSources * 数据 * @param * 泛型 * @return List */ public static List distinct(List dataSources) { return newStream(dataSources).filter(Objects::nonNull).distinct().collect(Collectors.toList()); } /** * 去重 * * @param dataSources * 数据 * @param * 泛型 * @return List */ public static List distinct(List dataSources, Function mapper) { List rs = mapToList(dataSources, mapper); return newStream(rs).filter(Objects::nonNull).distinct().collect(Collectors.toList()); } /** * 去重 * * @param dataSources * 数据 * @param * 泛型 * @return List */ public static List distinct(List dataSources, Function mapper1, Function mapper2) { List rs = mapToList(dataSources, mapper1); return newStream(rs).filter(Objects::nonNull).map(mapper2).distinct().collect(Collectors.toList()); } /** * 去重 * * @param dataSources * 数据 * @param * 泛型 * @return List */ public static List distinctByKey(List dataSources, Function mapper) { Map dismantling = dismantlingFirst(dataSources, mapper); return getValues(dismantling); } /** * 从流中获取Inter类型数据,非空处理后,求和 * * @param dataStream * 原始数据流 * @param mapper * 指定数字类型字段 * @param * 泛型 * @return Integer */ public static Integer mapToNonNullIntSum(Stream dataStream, Function mapper) { List integers = mapToList(dataStream, mapper); int total = 0; for (Integer integer : integers) { if (Objects.nonNull(integer)) { total += integer; } } return total; } /** * 从流中获取Inter类型数据,非空处理后,求和(如果元素为空,则返回空) * * @param dataStream * 原始数据流 * @param mapper * 指定数字类型字段 * @param * 泛型 * @return Integer */ public static Integer mapToNullAbleIntSum(Stream dataStream, Function mapper) { List integers = mapToList(dataStream, mapper); int total = 0; boolean haveNonNull = false; for (Integer integer : integers) { if (Objects.nonNull(integer)) { haveNonNull = true; total += integer; } } return haveNonNull ? total : null; } /** * 从流中获取Inter类型数据,非空处理后,求和 * * @param dataStream * 原始数据流 * @param mapper * 指定数字类型字段 * @param * 泛型 * @return Integer */ public static Long mapToNonNullLongSum(Stream dataStream, Function mapper) { List longs = mapToList(dataStream, mapper); long total = 0; for (Long nextLong : longs) { if (Objects.nonNull(nextLong)) { total += nextLong; } } return total; } /** * 从流中获取Inter类型数据,非空处理后,求和 * * @param dataSources * 原始数据源 * @param mapper * 指定数字类型字段 * @param * 泛型 * @return Integer */ public static Integer mapToNonNullIntSum(List dataSources, Function mapper) { return mapToNonNullIntSum(newStream(dataSources), mapper); } /** * 从流中获取Inter类型数据,非空处理后,求和(如果元素为空,则返回空) * * @param dataSources * 原始数据源 * @param mapper * 指定数字类型字段 * @param * 泛型 * @return Integer */ public static Integer mapToNullAbleIntSum(List dataSources, Function mapper) { return mapToNullAbleIntSum(newStream(dataSources), mapper); } /** * 从流中获取Inter类型数据,非空处理后,求和 * * @param dataSources * 原始数据源 * @param mapper * 指定数字类型字段 * @param * 泛型 * @return Integer */ public static Long mapToNonNullLongSum(List dataSources, Function mapper) { return mapToNonNullLongSum(newStream(dataSources), mapper); } /** * 从map中获取所有的值的list * * @param map * Map数据 * @param * key泛型 * @param * value泛型 * @return List */ public static List getValues(Map map) { if (CollectionUtils.isEmpty(map)) { return Collections.emptyList(); } Collection values = map.values(); return new ArrayList<>(values); } /** * 从map中获取所有的值的list * * @param map * Map数据 * @param * key泛型 * @param * value泛型 * @return List */ public static Optional getValuesFirst(Map map) { List values = getValues(map); return values.stream().findFirst(); } /** * 如果是数据分组格式key是一个,value是list的情况,只去value中的第一个。 * * @param map * Map数据 * @param * key泛型 * @param * value泛型 * @return List */ public static List eachGetValueFirst(Map> map) { if (CollectionUtils.isEmpty(map)) { return Collections.emptyList(); } List result = new ArrayList<>(map.size()); for (Map.Entry> entry : map.entrySet()) { if (!CollectionUtils.isEmpty(entry.getValue())) { result.add(entry.getValue().get(0)); } } return result; } /** * 从map中获取所有的key的list * * @param map * Map数据 * @param * key泛型 * @param * value泛型 * @return List */ public static List getKeys(Map map) { if (CollectionUtils.isEmpty(map)) { return Collections.emptyList(); } Set ks = map.keySet(); return new ArrayList<>(ks); } /** * 从流中建立新的类型 * * @param dataStream * 数据流 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List mapToList(Stream dataStream, Function mapping) { return newStream(dataStream).map(mapping).filter(Objects::nonNull).collect(Collectors.toList()); } /** * 从流中建立新的类型 * * @param dataSources * 数据源 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List mapToList(List dataSources, Function mapping) { return newStream(dataSources).map(mapping).filter(Objects::nonNull).collect(Collectors.toList()); } /** * 从流中建立新的类型 * * @param dataSources * 数据源 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List mapToUniqueList(List dataSources, Function mapping) { return newStream(dataSources).map(mapping).filter(Objects::nonNull).distinct().collect(Collectors.toList()); } /** * 从流中建立新的类型 * * @param dataSources * 数据源 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List mapToUniqueList(List dataSources, Function mapping, Function mapping1) { return newStream(dataSources).map(mapping).filter(Objects::nonNull).map(mapping1).filter(Objects::nonNull) .distinct().collect(Collectors.toList()); } /** * 从流中建立新的类型 * * @param dataSources * 数据源 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List supperMapToList(List dataSources, Function mapping, Function mapping2) { return newStream(dataSources).map(mapping).filter(Objects::nonNull).map(mapping2).filter(Objects::nonNull) .distinct().collect(Collectors.toList()); } /** * 将多个list集合合并成一个 如果要保证添加顺序,请按照顺序添加 * * @param dataSourceList * 数据源 * @return List */ @SafeVarargs public static List mergeList(List... dataSourceList) { if (Objects.isNull(dataSourceList)) { return emptyList(); } List result = new ArrayList<>(); for (List ts : dataSourceList) { if (isNotEmpty(ts)) { result.addAll(ts); } } return result; } /** * 从流中建立新的List类型,并将新的list * * @param dataSources * 数据源 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List mapToMergeList(List dataSources, Function> mapping) { List> collect = newStream(dataSources).map(mapping).filter(Objects::nonNull).collect(Collectors.toList()); List allList = new ArrayList<>(); for (List rs : collect) { allList.addAll(rs); } return allList; } /** * 从流中建立新的类型 * * @param dataSource * 数据 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static Set mapToSet(List dataSource, Function mapping) { return newStream(dataSource).map(mapping).filter(Objects::nonNull).collect(Collectors.toSet()); } /** * 从流中建立新的类型 * * @param dataStream * 数据流 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static Set mapToSet(Stream dataStream, Function mapping) { return newStream(dataStream).map(mapping).filter(Objects::nonNull).collect(Collectors.toSet()); } /** * 从流中建立新的类型并去重 * * @param dataSources * 数据流 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List distinctMapToList(List dataSources, Function mapping) { return newStream(newStream(dataSources)).map(mapping).filter(Objects::nonNull).distinct() .collect(Collectors.toList()); } /** * 从流中建立新的类型并去重 * * @param dataStream * 数据流 * @param mapping * 转换函数 * @param * 原数据类型 * @param * 目标数据类型 * @return List */ public static List distinctMapToList(Stream dataStream, Function mapping) { return newStream(dataStream).map(mapping).filter(Objects::nonNull).distinct().collect(Collectors.toList()); } /** * 对数据流进行过滤 * * @param dataStream * 数据流 * @param predicate * 过滤条件 * @param exceptionSupplier * 异常生成 * @param * 数据泛型 * @param * 异常泛型 * @return List 过滤后的数据条件 * @throws X * Throwable */ public static T filterSingle(Stream dataStream, Predicate<? super T> predicate, Supplier<? extends X> exceptionSupplier) throws X { return StreamFilter.filterSingle(newStream(dataStream), predicate, exceptionSupplier); } /** * 过滤 * * @param dataSource * 数据源 * @param predicate * 过滤条件 * @param * 数据泛型 * @return List */ public static List superFilter(List dataSource, Function function, Predicate<? super R> predicate) { return superFilter(newStream(dataSource), function, predicate); } /** * 根据条件生成List集合 * * @param dataStream * 数据流 * @param predicate * 过滤条件 * @param * 数据泛型 * @return List */ public static List superFilter(Stream dataStream, Function function, Predicate<? super R> predicate) { List result = new ArrayList<>(); dataStream.filter(Objects::nonNull).forEach(t -> { R apply = function.apply(t); if (predicate.test(apply)) { result.add(t); } }); return result; } /** * 对数据流进行过滤 * * @param dataSource * 数据源头 * @param predicate * 过滤条件 * @param * 数据泛型 * @return List 过滤后的数据条件 */ public static List filter(List dataSource, Predicate<? super T> predicate) { return StreamFilter.filter(newStream(dataSource), predicate); } /** * 对数据流进行过滤 * * @param dataStream * 数据流 * @param predicate * 过滤条件 * @param * 数据泛型 * @return List 过滤后的数据条件 */ public static List filter(Stream dataStream, Predicate<? super T> predicate) { return StreamFilter.filter(newStream(dataStream), predicate); } public static T findFirst(Stream dataStream, Supplier<? extends X> exceptionSupplier) throws X { return newStream(dataStream).findFirst().orElseThrow(exceptionSupplier); } public static Optional findFirst(Stream dataStream) { return newStream(dataStream).findFirst(); } public static Optional findFirst(List dataList) { return newStream(dataList).findFirst(); } public static Optional findAny(Stream dataStream) { return newStream(dataStream).findAny(); } /** * 从流中获取任意数据 * * @param dataList * 数据集合 * @param * 泛型 * @return Optional */ public static Optional findAny(List dataList) { return newStream(dataList).findAny(); } /** * 从流中获取任意数据 * * @param dataList * 数据集合 * @param * 泛型 * @return Optional */ public static T findRequiredAny(List dataList) { Optional any = newStream(dataList).findAny(); if (!any.isPresent()) { throw new RuntimeException("EnhanceStream.findRequiredAny:"+dataList); } return any.get(); } /** * 对数据流进行过滤,并对过滤后的数据进行类型转换 * * @param dataStream * 数据流 * @param predicate * 过滤条件 * @param applyMapping * 数据转换器 * @param * 数据泛型 * @param * 转换后的数据类型 * @return List 过滤后的数据条件 */ public static List filter(Stream dataStream, Predicate<? super T> predicate, Function<? super T, ? extends V> applyMapping) { return StreamFilter.filter(newStream(dataStream), predicate, applyMapping); } /** * 对数据流进行分组 * * @param dataStream * 数据流 * @param keyApply * 分组项 * @param * 数据泛型 * @param * 分组项泛型 * @return Map 分组后数据 */ public static Map> group(Stream dataStream, Function<? super T, ? extends K> keyApply) { return StreamBinder.group(newStream(dataStream), keyApply); } /** * 对数据流进行分组,并对分组后的数据进行类型转换 * * @param dataStream * 数据流 * @param keyApply * 分组项 * @param valueApply * 数据转换器 * @param * 数据泛型 * @param * 分组项泛型 * @param * 转换后的数据类型 * @return Map 分组后数据 */ public static Map> group(Stream dataStream, Function<? super T, ? extends K> keyApply, Function<? super T, ? extends V> valueApply) { return StreamBinder.group(newStream(dataStream), keyApply, valueApply); } /** * @param dataSource * 商品配置 * @param keyApply * key生成 * @param * key泛型 * @param * 数据源泛型 * @return Map */ public static Map dismantlingFirst(List dataSource, Function<? super T, ? extends K> keyApply) { return StreamBinder.dismantlingFirst(newStream(dataSource), keyApply); } /** * @param dataSource * 商品配置 * @param keyApply * key生成 * @param * key泛型 * @param * 数据源泛型 * @return Map */ public static Map dismantlingFirst(List dataSource, Function<? super T, ? extends K> keyApply,Function<? super T, ? extends V> valueApply) { return StreamBinder.dismantlingFirst(newStream(dataSource), keyApply,valueApply); } /** * @param dataSourceStream * 商品配置 * @param keyApply * key生成 * @param * key泛型 * @param * 数据源泛型 * @return Map */ public static Map dismantlingFirst(Stream dataSourceStream, Function<? super T, ? extends K> keyApply) { return StreamBinder.dismantlingFirst(dataSourceStream, keyApply); } /** * 数据拆分「注意: 拆分后的数据,如果Key一样会产生异常,请谨慎使用 * * @param dataStream * 数据流 * @param keyApply * Key转换器 * @param * 原始数据泛型 * @param * Key泛型 * @return Map 拆分后数据 */ public static Map dismantling(List dataStream, Function<? super T, ? extends K> keyApply) { return StreamBinder.dismantling(newStream(dataStream), keyApply); } /** * 数据拆分「注意: 拆分后的数据,如果Key一样会产生异常,请谨慎使用 * * @param dataStream * 数据流 * @param keyApply * Key转换器 * @param * 原始数据泛型 * @param * Key泛型 * @return Map 拆分后数据 */ public static Map dismantling(Stream dataStream, Function<? super T, ? extends K> keyApply) { return StreamBinder.dismantling(newStream(dataStream), keyApply); } /** * 数据拆分「注意: 拆分后的数据,如果Key一样会产生异常,请谨慎使用 * * @param dataSource * 数据源 * @param keyApply * Key转换器 * @param * 原始数据泛型 * @param * Key泛型 * @return Map 拆分后数据 */ public static Map dismantling(List dataSource, Function<? super T, ? extends K> keyApply, Supplier<? extends X> exceptionSupplier) throws X { return StreamBinder.dismantling(newStream(dataSource), keyApply, exceptionSupplier); } /** * 数据拆分「注意: 拆分后的数据,如果Key一样会产生异常,请谨慎使用 * * @param dataStream * 数据流 * @param keyApply * Key转换器 * @param * 原始数据泛型 * @param * Key泛型 * @return Map 拆分后数据 */ public static Map dismantling(Stream dataStream, Function<? super T, ? extends K> keyApply, Supplier<? extends X> exceptionSupplier) throws X { return StreamBinder.dismantling(newStream(dataStream), keyApply, exceptionSupplier); } /** * 数据拆分「注意: 拆分后的数据,如果Key一样会产生异常,请谨慎使用 * * @param dataStream * 数据流 * @param keyApply * Key转换器 * @param valueApply * Value转换器 * @param * 原始数据泛型 * @param * Key泛型 * @param * Value泛型 * @return Map 拆分后数据 */ public static Map dismantling(Stream dataStream, Function<? super T, ? extends K> keyApply, Function<? super T, ? extends V> valueApply) { return StreamBinder.dismantling(newStream(dataStream), keyApply, valueApply); } /** * 数据拆分「注意: 拆分后的数据,如果Key一样会产生异常,请谨慎使用 * * @param dataStream * 数据流 * @param keyApply * Key转换器 * @param valueApply * Value转换器 * @param * 原始数据泛型 * @param * Key泛型 * @param * Value泛型 * @param * 数据重复产生的异常描述 * @return Map 拆分后数据 */ public static Map dismantling(Stream dataStream, Function<? super T, ? extends K> keyApply, Function<? super T, ? extends V> valueApply, Supplier<? extends X> exceptionSupplier) throws X { return StreamBinder.dismantling(newStream(dataStream), keyApply, valueApply, exceptionSupplier); } private static Stream newStream(Stream dataSourcesStream) { if (dataSourcesStream == null) { return Stream.empty(); } return dataSourcesStream; } public static Stream newStream(List dataSources) { if (dataSources == null) { return Stream.empty(); } return dataSources.stream(); } /** * 对数据流进行过滤,并对过滤后的数据进行类型转换 * * @param dataSources * 原始数据流 * @param predicate * 过滤条件 * @param applyMapping * 数据转换器 * @param * 转换后的数据类型 * @param * 原始数据类型 * @return List 过滤后的数据条件 */ public static List filter(List dataSources, Predicate<? super T> predicate, Function<? super T, ? extends V> applyMapping) { return StreamFilter.filter(newStream(dataSources), predicate, applyMapping); } /** * 数据拆分「注意: 拆分后的数据,如果Key一样会产生异常,请谨慎使用 * * @param dataSources * 原始数据 * @param keyApply * Key转换器 * @param valueApply * Value转换器 * @param * Key泛型 * @param * Value泛型 * @param * 原始数据类型 * @return Map 拆分后数据 */ public static Map dismantling(List dataSources, Function<? super T, ? extends K> keyApply, Function<? super T, ? extends V> valueApply) { return StreamBinder.dismantling(newStream(dataSources), keyApply, valueApply); } public static void removeIf(List dataSources, Predicate predicate) { if (Objects.nonNull(dataSources) && !dataSources.isEmpty()) { dataSources.removeIf(predicate); } } public static void removeNotIf(List dataSources, Predicate predicate) { if (Objects.nonNull(dataSources) && !dataSources.isEmpty()) { dataSources.removeIf(t -> !predicate.test(t)); } } /** * 对数据流进行分组 * * @param dataSources * 原始数据流 * @param keyApply * 分组项 * @param * 分组项泛型 * @return Map 分组后数据 */ public static Map> group(List dataSources, Function<? super T, ? extends K> keyApply) { return StreamBinder.group(newStream(dataSources), keyApply); } /** * 对数据流进行分组,并对分组后的数据进行类型转换 * * @param dataSources * 原始数据流 * @param keyApply * 分组项 * @param valueApply * 数据转换器 * @param * 分组项泛型 * @param * 转换后的数据类型 * @return Map 分组后数据 */ public static Map> group(List dataSources, Function<? super T, ? extends K> keyApply, Function<? super T, ? extends V> valueApply) { return StreamBinder.group(newStream(dataSources), keyApply, valueApply); } /** * 升序 * * @param dataSources * 数据集合 * @param compareFunction * 排序字段 * @param * 集合类型泛型类 * @return List */ public static > List sortedAsc(List dataSources, Function compareFunction) { return sorted(newStream(dataSources), compareFunction, SortStrategy.UP, SortNullStrategy.NULL_IGNORE); } /** * 升序 * * @param dataStream * 数据集合 * @param compareFunction * 排序字段 * @param * 集合类型泛型类 * @return List */ public static > List sortedAsc(Stream dataStream, Function compareFunction) { return sorted(dataStream, compareFunction, SortStrategy.UP, SortNullStrategy.NULL_IGNORE); } /** * 升序 * * @param dataSources * 数据集合 * @param compareFunction * 排序字段 * @param sortNullStrategy * 排序控制策略 * @param * 集合类型泛型类 * @return List */ public static > List sortedAsc(List dataSources, Function compareFunction, SortNullStrategy sortNullStrategy) { return sorted(newStream(dataSources), compareFunction, SortStrategy.UP, sortNullStrategy); } /** * 升序 * * @param dataStream * 数据集合 * @param compareFunction * 排序字段 * @param * 集合类型泛型类 * @return List */ public static > List sortedAsc(Stream dataStream, Function compareFunction, SortNullStrategy sortNullStrategy) { return sorted(dataStream, compareFunction, SortStrategy.UP, sortNullStrategy); } /** * 降序 * * @param dataSources * 数据集合 * @param compareFunction * 排序字段 * @param * 集合类型泛型类 * @return List */ public static > List sortedDesc(List dataSources, Function compareFunction) { return sorted(newStream(dataSources), compareFunction, SortStrategy.DOWN, SortNullStrategy.NULL_IGNORE); } /** * 降序 * * @param dataStream * 数据集合 * @param compareFunction * 排序字段 * @param * 集合类型泛型类 * @return List */ public static > List sortedDesc(Stream dataStream, Function compareFunction) { return sorted(dataStream, compareFunction, SortStrategy.DOWN, SortNullStrategy.NULL_IGNORE); } /** * 降序 * * @param dataSources * 数据集合 * @param compareFunction * 排序字段 * @param * 集合类型泛型类 * @return List */ public static > List sortedDesc(List dataSources, Function compareFunction, SortNullStrategy sortNullStrategy) { return sorted(newStream(dataSources), compareFunction, SortStrategy.DOWN, sortNullStrategy); } /** * 降序 * * @param dataStream * 数据集合 * @param compareFunction * 排序字段 * @param * 集合类型泛型类 * @return List */ public static > List sortedDesc(Stream dataStream, Function compareFunction, SortNullStrategy sortNullStrategy) { return sorted(dataStream, compareFunction, SortStrategy.DOWN, sortNullStrategy); } private enum SortStrategy { /** * 升序 */ UP, /** * 降序 */ DOWN; } public enum SortNullStrategy { /** * 如果值为空就忽略 */ NULL_IGNORE, /** * 值为空就放最后 */ NULL_LAST, /** * 值为空就放前面 */ NULL_FIRST; } /** * @param dataStream * 数据集合 * @param compareFunction * 排序字段 * @param sortStrategy * 排序策略 * @param sortStrategy * true报异常 * @param * 集合类型泛型类 * @return List */ private static > List sorted(Stream dataStream, Function compareFunction, SortStrategy sortStrategy, SortNullStrategy sortNullStrategy) { Comparator comparing; if (Objects.isNull(sortStrategy) || SortStrategy.UP == sortStrategy) { if (sortNullStrategy == SortNullStrategy.NULL_LAST) { comparing = Comparator.comparing(compareFunction, Comparator.nullsLast(U::compareTo)); } else if (sortNullStrategy == SortNullStrategy.NULL_FIRST) { comparing = Comparator.comparing(compareFunction, Comparator.nullsFirst(U::compareTo)); } else { comparing = Comparator.comparing(compareFunction); } } else { if (sortNullStrategy == SortNullStrategy.NULL_LAST) { comparing = Comparator.comparing(compareFunction, Comparator.nullsLast(U::compareTo).reversed()); } else if (sortNullStrategy == SortNullStrategy.NULL_FIRST) { comparing = Comparator.comparing(compareFunction, Comparator.nullsFirst(U::compareTo).reversed()); } else { comparing = Comparator.comparing(compareFunction, Comparator.reverseOrder()); } } return dataStream.sorted(comparing).collect(Collectors.toList()); } public static boolean contains(T[] dataSources, T... elements) { return contains(Stream.of(dataSources), elements); } public static boolean containsAny(T[] dataSources, T... elements) { return containsAny(Stream.of(dataSources), elements); } public static boolean contains(List dataSource, T... elements) { return contains(newStream(dataSource), elements); } public static boolean containsAny(List dataSource, T... elements) { return containsAny(newStream(dataSource), elements); } public static boolean contains(Stream dataStream, T... elements) { List ts = mapToList(newStream(dataStream), Function.identity()); if (Objects.isNull(elements)) { return false; } for (T element : elements) { if (!ts.contains(element)) { return false; } } return true; } public static boolean containsAny(Stream dataStream, T... elements) { List ts = mapToList(dataStream, Function.identity()); if (Objects.isNull(elements)) { return false; } for (T element : elements) { if (ts.contains(element)) { return true; } } return false; } @SafeVarargs public static List merge(Function mapper, List... lists) { List result = new ArrayList<>(); for (R r : distinct(lists[0], mapper)) { boolean temp = true; for (int i = 1; i < lists.length; i++) { List rs = EnhanceStream.distinct(lists[i], mapper); Map reMapping = EnhanceStream.dismantling(rs, Function.identity()); if (Objects.isNull(reMapping.get(r))) { temp = false; } } if (temp) { result.add(r); } } return EnhanceStream.distinct(result); } public static T[] listToArr(List dataList) { return (T[])dataList.toArray(new Object[0]); } public static List arrToList(T[] dataList) { return Stream.of(dataList).collect(Collectors.toList()); }

有兴趣可以一起交流工具类,请关注我得公众号