All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.azbh111.utils.java.iterable.IterableUtils Maven / Gradle / Ivy

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 it) {
        return it == null || !it.iterator().hasNext();
    }

    public static  boolean isNotNullOrEmpty(Iterable 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 it, Function keyMapper) {
        return groupingBy(it, keyMapper, Function.identity());
    }

    public static  Map> groupingBy(Iterator it, Function keyMapper) {
        return groupingBy(it, keyMapper, Function.identity());
    }

    public static  Map> groupingBy(T[] it, Function keyMapper) {
        return groupingBy(it, keyMapper, Function.identity());
    }

    public static  Map> groupingBy(Stream it, Function keyMapper) {
        return groupingBy(it, keyMapper, Function.identity());
    }

    public static  Map> groupingBy(Iterable it, Function keyMapper, Function valueMapper) {
        return groupingBy(toStream(it), keyMapper, valueMapper);
    }

    public static  Map> groupingBy(Iterator it, Function keyMapper, Function valueMapper) {
        return groupingBy(toStream(it), keyMapper, valueMapper);
    }

    public static  Map> groupingBy(T[] it, Function keyMapper, Function valueMapper) {
        return groupingBy(toStream(it), keyMapper, valueMapper);
    }

    public static  Map> groupingBy(Stream it, Function keyMapper, Function valueMapper) {
        return groupingBy(it, new HashMap<>(), keyMapper, valueMapper);
    }

    public static  Map> groupingBy(Stream it, Map> resultContainer, Function keyMapper, Function 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 collector) {
        return collect(toStream(it), collector);
    }

    public static  R collect(Iterable it, Collector collector) {
        return collect(toStream(it), collector);
    }

    public static  R collect(Iterator it, Collector collector) {
        return collect(toStream(it), collector);
    }

    public static  R collect(Stream it, Collector collector) {
        return it.collect(collector);
    }

    // ====================collect==========================


    // ====================toSet==========================
    public static  Set toSet(Iterable it) {
        return toSet(it, Function.identity());
    }

    public static  Set toSet(T[] it) {
        return toSet(it, Function.identity());
    }

    public static  Set toSet(Stream it) {
        return toSet(it, Function.identity());
    }

    public static  Set toSet(Iterable it, Function func) {
        return toSet(toStream(it), func);
    }

    public static  Set toSet(Iterable it, Function func, Predicate predicate) {
        return toSet(toStream(it), func, predicate);
    }

    public static  Set toSet(T[] it, Function func) {
        return toSet(toStream(it), func);
    }

    public static  Set toSet(Stream it, Function func) {
        return it.map(func).collect(Collectors.toSet());
    }

    public static  Set toSet(Stream it, Function func, Predicate predicate) {
        return it.filter(predicate).map(func).collect(Collectors.toSet());
    }


    // ====================toSet==========================


    // ====================flatToList==========================
    public static  List flatToList(Iterable it, Function> func) {
        return flatToList(toStream(it), func);
    }

    public static  List flatToList(T[] it, Function> func) {
        return flatToList(toStream(it), func);
    }

    public static  List flatToList(Stream it, Function> func) {
        return it
                .flatMap(item -> toStream(func.apply(item)))
                .collect(Collectors.toList());
    }
    // ====================flatToList==========================

    // ====================flatToSet==========================
    public static  Set flatToSet(Iterable it, Function> func) {
        return flatToSet(toStream(it), func);
    }

    public static  Set flatToSet(T[] it, Function> func) {
        return flatToSet(toStream(it), func);
    }

    public static  Set flatToSet(Stream it, Function> func) {
        return it
                .flatMap(item -> toStream(func.apply(item)))
                .collect(Collectors.toSet());
    }
    // ====================flatToList==========================

    // ====================toList==========================
    public static  List toList(Iterable it) {
        return toList(it, Function.identity());
    }

    public static  List toList(T[] it) {
        return toList(it, Function.identity());
    }

    public static  List toList(Stream it) {
        return toList(it, Function.identity());
    }

    public static  List toList(Iterable it, Function func) {
        return toList(toStream(it), func);
    }

    public static  List toList(T[] it, Function func) {
        return toList(toStream(it), func);
    }

    public static  List toList(Stream it, Function func) {
        return it
                .map(func)
                .collect(Collectors.toList());
    }
    // ====================toList==========================


    // ====================toDistinctedList==========================
    public static  List toDistinctedList(Iterable it) {
        return toStream(it).distinct().collect(Collectors.toList());
    }

    public static  List toDistinctedList(Iterable it, Function func) {
        return toDistinctedList(toStream(it), func);
    }

    public static  List toDistinctedList(Stream it, Function func) {
        return it
                .map(func)
                .distinct()
                .collect(Collectors.toList());
    }

    public static  List toDistinctedListBy(Iterable it, Function func) {
        return toDistinctedListBy(toStream(it), func);
    }

    public static  List toDistinctedListBy(Stream it, Function func) {
        List list = new ArrayList<>();
        Predicate test = PredicateUtils.distinctBy(func);
        it.forEach(item -> {
            if (test.test(item)) {
                list.add(item);
            }
        });
        return list;
    }
    // ====================toDistinctedList==========================

    // ====================toMap==========================
    public static  Map toMap(Iterable it, Function keyMapper) {
        return toMap(it, keyMapper, Function.identity());
    }

    public static  Map toMap(T[] it, Function keyMapper) {
        return toMap(it, keyMapper, Function.identity());
    }

    public static  Map toMap(Stream it, Function keyMapper) {
        return toMap(it, keyMapper, Function.identity());
    }

    public static  Map toMap(Iterable it, Function keyMapper, Function valueMapper) {
        return toMap(toStream0(it), keyMapper, valueMapper);
    }

    public static  Map toMap(T[] it, Function keyMapper, Function valueMapper) {
        return toMap(toStream(it), keyMapper, valueMapper);
    }

    public static  Map toMap(Stream it, Function keyMapper, Function valueMapper) {
        return it.collect(Collectors.toMap(keyMapper, valueMapper));
    }
    // ====================toMap==========================


    // ====================toStream==========================

    public static  Stream toStream(Iterator it, Iterator... its) {
        Stream stream = toStream0(it);
        if (its == null) {
            return stream;
        }
        for (Iterator ts : its) {
            stream = Stream.concat(stream, toStream0(ts));
        }
        return stream;
    }

    public static  Stream toStream(Iterable it, Iterable... its) {
        Stream stream = toStream0(it);
        if (its == null) {
            return stream;
        }
        for (Iterable ts : its) {
            stream = Stream.concat(stream, toStream0(ts));
        }
        return stream;
    }

    public static  Stream toStream(T[] it, T[]... its) {
        Stream stream = toStream0(it);
        if (its == null) {
            return stream;
        }
        for (T[] ts : its) {
            stream = Stream.concat(stream, toStream0(ts));
        }
        return stream;
    }

    public static  Stream toStream(Stream s, Stream... ss) {
        Stream stream = s == null ? Stream.empty() : s;
        for (Stream ts : ss) {
            stream = Stream.concat(stream, ts);
        }
        return stream;
    }

    private static  Stream toStream0(Iterator it) {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false);
    }

    private static  Stream toStream0(Iterable it) {
        if (it == null) {
            return Stream.empty();
        }
        return StreamSupport.stream(it.spliterator(), false);
    }

    private static  Stream toStream0(T[] it) {
        if (it == null) {
            return Stream.empty();
        }
        return StreamSupport.stream(Arrays.spliterator(it), false);
    }


    // ====================max==========================
    public static  T max(Iterable it) {
        return max(it, Comparable::compareTo);
    }

    public static  T max(T[] it) {
        return max(it, Comparable::compareTo);
    }

    public static  T max(Stream it) {
        return max(it, Comparable::compareTo);
    }

    public static  T max(Iterable it, Comparator comparator) {
        return max(toStream(it), comparator);
    }

    public static  T max(T[] it, Comparator comparator) {
        return max(toStream(it), comparator);
    }

    public static  T max(Stream it, Comparator comparator) {
        return it.max(comparator).orElse(null);
    }
    // ====================max==========================


    // ====================min==========================
    public static  T min(Iterable it) {
        return min(it, Comparable::compareTo);
    }

    public static  T min(T[] it) {
        return min(it, Comparable::compareTo);
    }

    public static  T min(Stream it) {
        return min(it, Comparable::compareTo);
    }

    public static  T min(Iterable it, Comparator comparator) {
        return min(toStream(it), comparator);
    }

    public static  T min(T[] it, Comparator comparator) {
        return min(toStream(it), comparator);
    }

    public static  T min(Stream it, Comparator comparator) {
        return it.min(comparator).orElse(null);
    }

    // ====================min==========================

    public static class StreamSpliter implements Iterator> {
        private Iterator it;
        private int batchSize;
        private List batchList;

        public StreamSpliter(Stream it, int batchSize) {
            this.it = it.iterator();
            this.batchSize = batchSize;
            prepareNext();
        }

        @Override
        public boolean hasNext() {
            return batchList != null && !batchList.isEmpty();
        }

        private void prepareNext() {
            batchList = new ArrayList<>(batchSize);
            while (batchList.size() < batchSize && it.hasNext()) {
                batchList.add(it.next());
            }
        }

        @Override
        public List next() {
            List re = batchList;
            prepareNext();
            return re;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy