com.github.azbh111.utils.java.iterable.IterableUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-java Show documentation
Show all versions of utils-java Show documentation
com.github.azbh111:utils-java
The newest version!
/*
* Copyright (c) 2014-2019, Deepspring Healthcare Inc. All rights reserved.
*/
package com.github.azbh111.utils.java.iterable;
import com.github.azbh111.utils.java.annotation.Nonnull;
import com.github.azbh111.utils.java.annotation.Nullable;
import com.github.azbh111.utils.java.exception.ExceptionUtils;
import com.github.azbh111.utils.java.iterable.model.DifferenceView;
import com.github.azbh111.utils.java.iterable.model.IteratorMapper;
import com.github.azbh111.utils.java.iterable.model.MergedLazyIterator;
import com.github.azbh111.utils.java.iterable.model.ObjectSumFunction;
import com.github.azbh111.utils.java.map.MapUtils;
import com.github.azbh111.utils.java.predicate.PredicateUtils;
import com.github.azbh111.utils.java.reflect.ReflectUtils;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* @author pyz
* @date 2018/10/30 9:52 AM
*/
public class IterableUtils {
/**
* 将Iterator包装一下, 对每个元素进行转换, 返回转换后的Iterator, 结果是懒加载的
*
* @param source source
* @param mapper mapper
* @return java.util.Iterator
* @author zhengyongpan
* @since 2021/7/27 17:56
*/
public static Iterator mapIterator(Iterator source, Function mapper) {
return new IteratorMapper<>(source, mapper);
}
/**
* 将多个Supplier合并成1个, 合并结果是懒加载的
*
* @param sources sources
* @return com.github.azbh111.utils.java.iterable.model.MergedLazyIterator
* @author zhengyongpan
* @since 2021/7/27 17:55
*/
public static MergedLazyIterator mergeLazyIterator(Iterator>> sources) {
return new MergedLazyIterator<>(new IteratorMapper<>(sources, i -> i.get()));
}
/**
* 将多个Iterator合并成1个, 合并结果是懒加载的
*
* @param sources sources
* @return com.github.azbh111.utils.java.iterable.model.MergedLazyIterator
* @author zhengyongpan
* @since 2021/7/27 17:54
*/
public static MergedLazyIterator mergeIterator(Iterator> sources) {
return new MergedLazyIterator<>(sources);
}
/**
* 拆分数组
*
* @param it it
* @param batchSize batchSize
* @return java.util.List>
* @author zhengyongpan
* @since 2020/11/26 18:44
*/
public static List> splitBatch(T[] it, int batchSize) {
return splitBatch(toStream(it), batchSize);
}
/**
* 拆分集合
*
* @param it it
* @param batchSize batchSize
* @return java.util.List>
* @author zhengyongpan
* @since 2020/11/26 18:44
*/
public static List> splitBatch(Iterable it, int batchSize) {
return splitBatch(toStream(it), batchSize);
}
/**
* 拆分集合
*
* @param it it
* @param batchSize batchSize
* @return java.util.List>
* @author zhengyongpan
* @since 2020/11/26 18:44
*/
public static List> splitBatch(Iterator it, int batchSize) {
return splitBatch(toStream(it), batchSize);
}
/**
* 拆分集合
*
* @param it it
* @param batchSize batchSize
* @return java.util.List>
* @author zhengyongpan
* @since 2020/11/26 18:44
*/
public static List> splitBatch(Stream it, int batchSize) {
if (it == null) {
return new ArrayList<>();
}
List> container = new ArrayList<>();
List cache = new ArrayList<>(batchSize);
Iterator ite = it.iterator();
while (ite.hasNext()) {
cache.add(ite.next());
if (cache.size() == batchSize) {
container.add(cache);
cache = new ArrayList<>(batchSize);
}
}
if (!cache.isEmpty()) {
container.add(cache);
}
return container;
}
/**
* 拆分集合,返回Stream
* 用于大批量数据分批处理
* 结果是懒加载的
*
* @param it
* @param batchSize
* @param
* @return
*/
public static Stream> splitBatchStream(Stream it, int batchSize) {
if (it == null) {
return Stream.empty();
}
return toStream(new StreamSpliter<>(it, batchSize));
}
/**
* 拆分集合,返回Stream
* 用于大批量数据分批处理
*
* @param it
* @param batchSize
* @param
* @return
*/
public static Stream> splitBatchStream(T[] it, int batchSize) {
return splitBatchStream(toStream(it), batchSize);
}
/**
* 拆分集合,返回Stream
* 用于大批量数据分批处理
*
* @param it
* @param batchSize
* @param
* @return
*/
public static Stream> splitBatchStream(Iterable it, int batchSize) {
return splitBatchStream(toStream(it), batchSize);
}
/**
* 拆分集合,返回Stream
* 用于大批量数据分批处理
*
* @param it
* @param batchSize
* @param
* @return
*/
public static Stream> splitBatchStream(Iterator it, int batchSize) {
return splitBatchStream(toStream(it), batchSize);
}
/**
* 批量遍历
* 用于大批量数据分批处理
*
* @param stream stream
* @param batchSize batchSize
* @param consumer consumer
* @return int 遍历条数
* @author zhengyongpan
* @since 2020/11/26 18:42
*/
public static int batchLoop(T[] it, int batchSize, @Nonnull Consumer> consumer) {
return batchLoop(toStream(it), batchSize, consumer);
}
/**
* 批量遍历
* 用于大批量数据分批处理
*
* @param it it
* @param batchSize batchSize
* @param consumer consumer
* @return int 遍历条数
* @author zhengyongpan
* @since 2020/11/30 16:05
*/
public static int batchLoop(Iterable it, int batchSize, @Nonnull Consumer> consumer) {
return batchLoop(toStream(it), batchSize, consumer);
}
/**
* 批量遍历
* 用于大批量数据分批处理
*
* @param stream stream
* @param batchSize batchSize
* @param consumer consumer
* @return void
* @author zhengyongpan
* @since 2020/11/26 18:42
*/
public static int batchLoop(Iterator it, int batchSize, @Nonnull Consumer> consumer) {
return batchLoop(toStream(it), batchSize, consumer);
}
/**
* 批量遍历
*
* @param stream stream
* @param batchSize batchSize
* @param consumer consumer
* @return int 遍历条数
* @author zhengyongpan
* @since 2020/11/26 18:42
*/
public static int batchLoop(Stream stream, int batchSize, @Nonnull Consumer> consumer) {
if (stream == null) {
return 0;
}
int[] count = new int[1];
List cache = new ArrayList<>();
stream.forEach(i -> {
cache.add(i);
if (cache.size() == batchSize) {
count[0] += batchSize;
consumer.accept(cache);
cache.clear();
}
});
if (!cache.isEmpty()) {
count[0] += cache.size();
consumer.accept(cache);
}
return count[0];
}
/**
* 按照给定的顺序排序
* sortedKeys中没有的元素,会被过滤掉
*
* @param list 经过keyMapper映射,不能用重复的key
* @param sortedKeys
* @param keyMapper
* @param
* @param
* @return 排序后的集合
*/
public static List sortByKeys(@Nullable Iterable list, @Nonnull Collection sortedKeys, @Nonnull Function keyMapper) {
Map valMap = toStream(list).collect(Collectors.toMap(keyMapper, Function.identity()));
return sortedKeys.stream().map(i -> valMap.get(i)).filter(Objects::nonNull).collect(Collectors.toList());
}
/**
* 合并对象
*
* @param it
* @param container
* @param fieldFilter
* @param sumFunction
* @param
*/
public static void sum(Iterable it, @Nonnull T container, Predicate fieldFilter, @Nonnull ObjectSumFunction sumFunction) {
if (it == null) {
return;
}
if (isNullOrEmpty(it)) {
return;
}
T firstElement = getFirstElement(it);
List fields = ReflectUtils.getAllFields(firstElement.getClass(), fieldFilter);
for (Field field : fields) {
field.setAccessible(true);
}
for (T t : it) {
for (Field field : fields) {
try {
Object appendValue = field.get(t);
Object currentValue = field.get(container);
Object newValue = sumFunction.sum(field, currentValue, t, appendValue);
field.set(container, newValue);
} catch (IllegalAccessException e) {
ExceptionUtils.throwException(e);
}
}
}
}
/**
* 分组合并
*
* @param it
* @param container
* @param fieldFilter
* @param sumFunction
* @param
*/
public static Map sumByKey(Iterable it, Predicate fieldFilter, @Nonnull ObjectSumFunction sumFunction, @Nonnull Function keyMapper) {
return sumByKey(toStream(it), fieldFilter, sumFunction, keyMapper);
}
/**
* 分组合并
*
* @param it
* @param container
* @param fieldFilter
* @param sumFunction
* @param
*/
public static Map sumByKey(Iterator it, Predicate fieldFilter, @Nonnull ObjectSumFunction sumFunction, @Nonnull Function keyMapper) {
return sumByKey(toStream(it), fieldFilter, sumFunction, keyMapper);
}
/**
* 分组合并
*
* @param it
* @param container
* @param fieldFilter
* @param sumFunction
* @param
*/
public static Map sumByKey(Stream it, Predicate fieldFilter, @Nonnull ObjectSumFunction sumFunction, @Nonnull Function keyMapper) {
if (it == null) {
return new HashMap<>();
}
Iterator iter = it.iterator();
if (iter == null || !iter.hasNext()) {
return new HashMap<>();
}
List fields = null;
Map map = new HashMap<>();
try {
while (iter.hasNext()) {
T t = iter.next();
K key = keyMapper.apply(t);
T container = map.get(key);
if (container == null) {
container = (T) t.getClass().newInstance();
map.put(key, container);
}
if (fields == null) {
fields = ReflectUtils.getAllFields(t.getClass(), fieldFilter);
for (Field field : fields) {
field.setAccessible(true);
}
}
for (Field field : fields) {
Object appendValue = field.get(t);
Object currentValue = field.get(container);
Object newValue = sumFunction.sum(field, currentValue, t, appendValue);
field.set(container, newValue);
}
}
} catch (IllegalAccessException | InstantiationException e) {
ExceptionUtils.throwException(e);
}
return map;
}
/**
* 对Iterator进行计数
* 会将该Iterator剩余元素全部遍历一次
*
* @param iterator
* @param
* @return
*/
public static int count(Iterator iterator) {
if (iterator == null) {
return 0;
}
int count = 0;
while (iterator.hasNext()) {
iterator.next();
count++;
}
return count;
}
/**
* 对Iterable进行计数
*
* @param iterable
* @param
* @return
*/
public static int count(Iterable iterable) {
if (iterable == null) {
return 0;
}
if (iterable instanceof Collection) {
return ((Collection) iterable).size();
}
int count = 0;
for (T t : iterable) {
count++;
}
return count;
}
/**
* 对比两个集合的差异
*
* @param leftList
* @param rightList
* @param
* @return
*/
public static DifferenceView> difference(Set leftList, Set rightList) {
return difference(leftList, rightList, new HashSet(), new HashSet(), new HashSet(), Function.identity());
}
/**
* 对比两个集合的差异
*
* @param leftList
* @param rightList
* @param identityMapper
* @param
* @param
* @return
*/
public static DifferenceView> difference(Set leftList, Set rightList, Function identityMapper) {
return difference(leftList, rightList, new HashSet(), new HashSet(), new HashSet(), identityMapper);
}
/**
* 对比两个集合的差异
*
* @param leftList
* @param rightList
* @param
* @param
* @return
*/
public static DifferenceView> difference(List leftList, List rightList) {
return difference(leftList, rightList, Function.identity());
}
/**
* 对比两个集合的差异
*
* @param leftList
* @param rightList
* @param identityMapper
* @param
* @param
* @return
*/
public static DifferenceView> difference(List leftList, List rightList, Function identityMapper) {
return difference(leftList, rightList, new ArrayList(), new ArrayList(), new ArrayList(), identityMapper);
}
private static , T, K> DifferenceView difference(Collection leftList, Collection rightList, C left, C common, C right, Function identityMapper) {
leftList = leftList == null ? new ArrayList<>() : leftList;
rightList = rightList == null ? new ArrayList<>() : rightList;
Map leftMap = new LinkedHashMap<>();
for (T t : leftList) {
leftMap.put(identityMapper.apply(t), t);
}
for (T t : rightList) {
K k = identityMapper.apply(t);
if (leftMap.containsKey(k)) {
leftMap.remove(k);
common.add(t);
continue;
} else {
right.add(t);
}
}
left.addAll(leftMap.values());
return DifferenceView.builder()
.leftOnly(left)
.common(common)
.rightOnly(right)
.build();
}
public static void forEach(Consumer consumer, Iterable... its) {
if (its == null) {
return;
}
for (Iterable it : its) {
for (T t : it) {
consumer.accept(t);
}
}
}
/**
* 去重
*
* @param it
* @param
*/
public static void distinct(Iterable it) {
distinct(it, Function.identity());
}
/**
* 去重
*
* @param it
* @param distinctKey
* @param
* @param
*/
public static void distinct(Iterable it, Function distinctKey) {
if (it == null) {
return;
}
Iterator iterator = it.iterator();
Set set = new HashSet<>();
while (iterator.hasNext()) {
if (!set.add(distinctKey.apply(iterator.next()))) {
iterator.remove();
}
}
}
public static boolean isNullOrEmpty(Iterable extends T> it) {
return it == null || !it.iterator().hasNext();
}
public static boolean isNotNullOrEmpty(Iterable extends T> it) {
return it != null && it.iterator().hasNext();
}
/**
* 获取最后一个元素
*
* @param it
* @param
* @return
*/
public static T getLastElement(Iterable it) {
if (isNullOrEmpty(it)) {
return null;
}
if (it instanceof List) {
if (it instanceof Deque) {
Deque deque = (Deque) it;
return deque.getLast();
} else {
List list = (List) it;
return list.get(list.size() - 1);
}
}
T last = null;
for (T t : it) {
last = t;
}
return last;
}
/**
* 返回集合第一个元素(无序集合返回值不固定)
*
* @param it
* @param
* @return
*/
public static T getFirstElement(Iterable it) {
if (it == null) {
return null;
}
Iterator iterator = it.iterator();
return iterator.hasNext() ? iterator.next() : null;
}
public static List filter(Iterable it, Predicate test) {
return toStream(it)
.filter(test)
.collect(Collectors.toList());
}
public static Optional findFirst(Iterable it, Predicate test) {
return toStream(it).filter(test).findFirst();
}
/**
* 合计
*
* @param target
* @param partMap
* @param partSetter
* @param -
* @param
* @param
*/
public static - void aggregate(Map
target, Map partMap, BiConsumer- partSetter) {
for (Map.Entry
entry : target.entrySet()) {
Part part = partMap.get(entry.getKey());
if (part != null) {
partSetter.accept(entry.getValue(), part);
}
}
return;
}
/**
* 合计
*
* @param target
* @param parts
* @param keyGetter
* @param partSetter
* @param -
* @param
* @param
*/
public static - void aggregate(Map
target, Iterable parts, Function keyGetter, BiConsumer- partSetter) {
for (Part part : parts) {
Key key = keyGetter.apply(part);
Item item = target.get(key);
if (item != null) {
partSetter.accept(item, part);
}
}
return;
}
/**
* 合计
*
* @param target
* @param partMap
* @param keyGetter
* @param partSetter
* @param
-
* @param
* @param
*/
public static - void aggregate(Iterable
- target, Map
partMap, Function- keyGetter, BiConsumer
- partSetter) {
for (Item item : target) {
Part part = partMap.get(keyGetter.apply(item));
if (part == null) {
continue;
}
partSetter.accept(item, part);
}
return;
}
// =====================groupingBy=========================
public static
Map> groupingBy(Iterable extends T> it, Function super T, ? extends R> keyMapper) {
return groupingBy(it, keyMapper, Function.identity());
}
public static Map> groupingBy(Iterator extends T> it, Function super T, ? extends R> keyMapper) {
return groupingBy(it, keyMapper, Function.identity());
}
public static Map> groupingBy(T[] it, Function super T, ? extends R> keyMapper) {
return groupingBy(it, keyMapper, Function.identity());
}
public static Map> groupingBy(Stream extends T> it, Function super T, ? extends R> keyMapper) {
return groupingBy(it, keyMapper, Function.identity());
}
public static Map> groupingBy(Iterable extends T> it, Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper) {
return groupingBy(toStream(it), keyMapper, valueMapper);
}
public static Map> groupingBy(Iterator extends T> it, Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper) {
return groupingBy(toStream(it), keyMapper, valueMapper);
}
public static Map> groupingBy(T[] it, Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper) {
return groupingBy(toStream(it), keyMapper, valueMapper);
}
public static Map> groupingBy(Stream extends T> it, Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper) {
return groupingBy(it, new HashMap<>(), keyMapper, valueMapper);
}
public static Map> groupingBy(Stream extends T> it, Map> resultContainer, Function super T, ? extends K> keyMapper, Function super T, ? extends V> valueMapper) {
it.forEach(t -> {
// java stream api中的groupingBy要求key非空,这里同样
MapUtils.getOrCreate(resultContainer,
Objects.requireNonNull(keyMapper.apply(t), "element cannot be mapped to a null key"),
ArrayList::new)
.add(valueMapper.apply(t));
});
return resultContainer;
}
// ====================groupingBy==========================
// ====================collect==========================
public static R collect(T[] it, Collector super T, A, R> collector) {
return collect(toStream(it), collector);
}
public static R collect(Iterable it, Collector super T, A, R> collector) {
return collect(toStream(it), collector);
}
public static R collect(Iterator it, Collector super T, A, R> collector) {
return collect(toStream(it), collector);
}
public static R collect(Stream extends T> it, Collector super T, A, R> collector) {
return it.collect(collector);
}
// ====================collect==========================
// ====================toSet==========================
public static Set toSet(Iterable extends T> it) {
return toSet(it, Function.identity());
}
public static Set toSet(T[] it) {
return toSet(it, Function.identity());
}
public static Set toSet(Stream extends T> it) {
return toSet(it, Function.identity());
}
public static Set toSet(Iterable extends T> it, Function super T, ? extends R> func) {
return toSet(toStream(it), func);
}
public static Set toSet(Iterable extends T> it, Function super T, ? extends R> func, Predicate super T> predicate) {
return toSet(toStream(it), func, predicate);
}
public static Set toSet(T[] it, Function super T, ? extends R> func) {
return toSet(toStream(it), func);
}
public static Set toSet(Stream extends T> it, Function super T, ? extends R> func) {
return it.map(func).collect(Collectors.toSet());
}
public static Set toSet(Stream extends T> it, Function super T, ? extends R> func, Predicate super T> predicate) {
return it.filter(predicate).map(func).collect(Collectors.toSet());
}
// ====================toSet==========================
// ====================flatToList==========================
public static List flatToList(Iterable extends T> it, Function super T, Iterable> func) {
return flatToList(toStream(it), func);
}
public static List flatToList(T[] it, Function super T, Iterable> func) {
return flatToList(toStream(it), func);
}
public static List