com.livk.commons.util.BaseStreamUtils Maven / Gradle / Ivy
/*
* Copyright 2021-2024 spring-boot-extension the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.livk.commons.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import lombok.experimental.UtilityClass;
/**
*
* Stream工具类
*
*
* @author livk
*/
@UtilityClass
public class BaseStreamUtils {
/**
* 合并Map,key相同的则合并成List
* @param key type parameter
* @param value type parameter
* @param maps maps
* @return map
*/
@SuppressWarnings("varargs")
@SafeVarargs
public Map> concat(Map... maps) {
if (ObjectUtils.isEmpty(maps)) {
return Collections.emptyMap();
}
return Arrays.stream(maps)
.filter(Objects::nonNull)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
/**
* 合并数组
* @param type parameter
* @param ts the ts
* @return stream
*/
@SuppressWarnings("varargs")
@SafeVarargs
public Stream concat(T[]... ts) {
if (ObjectUtils.isEmpty(ts)) {
return Stream.empty();
}
return Arrays.stream(ts).filter(Objects::nonNull).flatMap(Arrays::stream);
}
/**
* 合并string数组
* @param distinct 是否去重
* @param strArr str[]
* @return string []
*/
public String[] concat(boolean distinct, String[]... strArr) {
Stream concat = concat(strArr);
if (distinct) {
concat = concat.distinct();
}
return concat.toArray(String[]::new);
}
/**
* 合并string数组并去重
* @param strArr str[]
* @return string []
*/
public String[] concatDistinct(String[]... strArr) {
return concat(true, strArr);
}
/**
* 合并int数组
* @param intArray int array
* @return int []
*/
public int[] concat(int[]... intArray) {
if (ObjectUtils.isEmpty(intArray)) {
return IntStream.empty().toArray();
}
return Arrays.stream(intArray).filter(Objects::nonNull).flatMapToInt(Arrays::stream).toArray();
}
/**
* 合并long数组
* @param longArray long array
* @return long []
*/
public long[] concat(long[]... longArray) {
if (ObjectUtils.isEmpty(longArray)) {
return LongStream.empty().toArray();
}
return Arrays.stream(longArray).filter(Objects::nonNull).flatMapToLong(Arrays::stream).toArray();
}
/**
* 合并double数组
* @param doubleArray double array
* @return double []
*/
public double[] concat(double[]... doubleArray) {
if (ObjectUtils.isEmpty(doubleArray)) {
return DoubleStream.empty().toArray();
}
return Arrays.stream(doubleArray).filter(Objects::nonNull).flatMapToDouble(Arrays::stream).toArray();
}
/**
* 通过function合并多个Stream
* @param 转换前泛型
* @param 转换后泛型
* @param combinator 转换Function
* @param streams 待转换Stream
* @return 合并后的Stream
*/
@SuppressWarnings("varargs")
@SafeVarargs
public Stream zip(Function, Stream> combinator, Stream... streams) {
return ObjectUtils.isEmpty(streams) ? Stream.empty() : Stream.of(streams).flatMap(combinator);
}
/**
* 根据某个条件进行去重
* @param type parameter
* @param function 去重条件
* @return predicate
*/
public Predicate distinct(Function super T, ?> function) {
Map