mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 15:55:18 +08:00
添加工具类
This commit is contained in:
80
src/main/java/cn/lihongjie/coal/common/CollectionUtils.java
Normal file
80
src/main/java/cn/lihongjie/coal/common/CollectionUtils.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package cn.lihongjie.coal.common;
|
||||
|
||||
import io.vavr.Tuple2;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
@UtilityClass
|
||||
public class CollectionUtils {
|
||||
|
||||
public static <Data, T> List<T> map(Iterable<Data> data, Function<Data, T> fn) {
|
||||
|
||||
return StreamSupport.stream(data.spliterator(), false).map(fn).collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
public static <A, B, K> List<Tuple2<A, B>> innerHashJoin(Iterable<A> a, Iterable<B> b, Function<A, K> ak, Function<B, K> bk) {
|
||||
Map<K, List<A>> aMap = newStream(a).collect(Collectors.groupingBy(ak));
|
||||
Map<K, List<B>> bMap = newStream(b).collect(Collectors.groupingBy(bk));
|
||||
return aMap.keySet().stream().flatMap(k -> aMap.getOrDefault(k, new ArrayList<>()).stream().flatMap(av -> bMap.getOrDefault(k, new ArrayList<>()).stream().map(bv -> new Tuple2<>(av, bv)))).collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
public static <A, B> List<Tuple2<A, B>> innerNestJoin(Iterable<A> a, Iterable<B> b, BiFunction<A, B, Boolean> test) {
|
||||
a = ObjectUtils.defaultIfNull(a, new ArrayList<>());
|
||||
b = ObjectUtils.defaultIfNull(b, new ArrayList<>());
|
||||
|
||||
List<Tuple2<A, B>> ans = new ArrayList<>();
|
||||
|
||||
for (A a1 : a) {
|
||||
for (B b1 : b) {
|
||||
if (test.apply(a1, b1)) {
|
||||
ans.add(new Tuple2<>(a1, b1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static <A, B, K> List<Tuple2<A, B>> leftHashJoin(Iterable<A> a, Iterable<B> b, Function<A, K> ak, Function<B, K> bk) {
|
||||
Map<K, List<A>> aMap = newStream(a).collect(Collectors.groupingBy(ak));
|
||||
Map<K, List<B>> bMap = newStream(b).collect(Collectors.groupingBy(bk));
|
||||
return aMap.keySet().stream().flatMap(k -> aMap.getOrDefault(k, new ArrayList<>()).stream().flatMap(av -> bMap.getOrDefault(k, List.of((B) null)).stream().map(bv -> new Tuple2<>(av, bv)))).collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
|
||||
public static <A, B> List<Tuple2<A, B>> leftNestJoin(Iterable<A> a, Iterable<B> b, BiFunction<A, B, Boolean> test) {
|
||||
a = ObjectUtils.defaultIfNull(a, new ArrayList<>());
|
||||
b = ObjectUtils.defaultIfNull(b, new ArrayList<>());
|
||||
|
||||
List<Tuple2<A, B>> ans = new ArrayList<>();
|
||||
|
||||
for (A a1 : a) {
|
||||
boolean find = false;
|
||||
for (B b1 : b) {
|
||||
if (test.apply(a1, b1)) {
|
||||
ans.add(new Tuple2<>(a1, b1));
|
||||
find = true;
|
||||
}
|
||||
}
|
||||
if (!find) {
|
||||
ans.add(new Tuple2<>(a1, null));
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
private static <A> Stream<A> newStream(Iterable<A> a) {
|
||||
return StreamSupport.stream(a == null ? new ArrayList<A>().spliterator() : a.spliterator(), false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user