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