【Java】LambdaStream

Java Lambda Stream Factory

import java.util.*;
import java.util.stream.*;

public class LambdaStream {

    public static <T> Stream<T> empty() {
        return Stream.empty();
    }

    public static <T> Stream<T> of(T t) {
        return Stream.of(t);
    }

    @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
    public static <T> Stream<T> of(Optional<T> optional) {
        return optional.map(Stream::of).orElseGet(Stream::empty);
    }

    public static <T> Stream<T> ofNullable(T t) {
        return t == null ? Stream.empty() : Stream.of(t);
    }

    @SuppressWarnings({"OptionalUsedAsFieldOrParameterType", "OptionalAssignedToNull"})
    public static <T> Stream<T> ofNullable(Optional<T> optional) {
        return optional == null ? Stream.empty() : optional.map(Stream::of).orElseGet(Stream::empty);
    }

    public static IntStream of(Spliterator.OfInt spliterator) {
        return StreamSupport.intStream(spliterator, false);
    }

    public static LongStream of(Spliterator.OfLong spliterator) {
        return StreamSupport.longStream(spliterator, false);
    }

    public static DoubleStream of(Spliterator.OfDouble spliterator) {
        return StreamSupport.doubleStream(spliterator, false);
    }

    public static <T> Stream<T> of(Spliterator<T> spliterator) {
        return StreamSupport.stream(spliterator, false);
    }

    public static <E> Stream<E> of(Iterator<E> iterator) {
        return of(Spliterators.spliteratorUnknownSize(iterator, Spliterator.IMMUTABLE));
    }

    public static <T> Stream<T> of(Iterable<T> iterable) {
        return of(Spliterators.spliteratorUnknownSize(iterable.iterator(), Spliterator.IMMUTABLE));
    }

    public static <E> Stream<E> of(Enumeration<E> enumeration) {
        return of(Spliterators.spliteratorUnknownSize(new Iterator<E>() {
            @Override
            public boolean hasNext() {
                return enumeration.hasMoreElements();
            }

            @Override
            public E next() {
                return enumeration.nextElement();
            }
        }, Spliterator.IMMUTABLE));
    }

    public static <E> Stream<E> of(Collection<E> collect) {
        return of(Spliterators.spliterator(collect, Spliterator.IMMUTABLE));
    }

    public static <K, V> Stream<Map.Entry<K, V>> of(Map<K, V> map) {
        return of(Spliterators.spliterator(map.entrySet(), Spliterator.IMMUTABLE));
    }

    @SafeVarargs
    public static <T> Stream<T> of(T... values) {
        return of(Arrays.spliterator(values));
    }

    public static <T> Stream<T> of(T[] array, int startInclusive, int endExclusive) {
        return of(Arrays.spliterator(array, startInclusive, endExclusive));
    }

    public static IntStream of(int[] array) {
        return of(Arrays.spliterator(array));
    }

    public static IntStream of(int[] array, int startInclusive, int endExclusive) {
        return of(Arrays.spliterator(array, startInclusive, endExclusive));
    }

    public static LongStream of(long[] array) {
        return of(Arrays.spliterator(array));
    }

    public static LongStream of(long[] array, int startInclusive, int endExclusive) {
        return of(Arrays.spliterator(array, startInclusive, endExclusive));
    }

    public static DoubleStream of(double[] array) {
        return of(Arrays.spliterator(array));
    }

    public static DoubleStream of(double[] array, int startInclusive, int endExclusive) {
        return of(Arrays.spliterator(array, startInclusive, endExclusive));
    }

    public static Stream<Integer> ofBoxed(int[] array) {
        return of(array).boxed();
    }

    public static Stream<Integer> ofBoxed(int[] array, int startInclusive, int endExclusive) {
        return of(array, startInclusive, endExclusive).boxed();
    }

    public static Stream<Long> ofBoxed(long[] array) {
        return of(array).boxed();
    }

    public static Stream<Long> ofBoxed(long[] array, int startInclusive, int endExclusive) {
        return of(array, startInclusive, endExclusive).boxed();
    }

    public static Stream<Double> ofBoxed(double[] array) {
        return of(array).boxed();
    }

    public static Stream<Double> ofBoxed(double[] array, int startInclusive, int endExclusive) {
        return of(array, startInclusive, endExclusive).boxed();
    }

    public static IntStream ofUnboxed(Integer[] array) {
        return of(array).mapToInt(Integer::intValue);
    }

    public static IntStream ofUnboxed(Integer[] array, int startInclusive, int endExclusive) {
        return of(array, startInclusive, endExclusive).mapToInt(Integer::intValue);
    }

    public static LongStream ofUnboxed(Long[] array) {
        return of(array).mapToLong(Long::longValue);
    }

    public static LongStream ofUnboxed(Long[] array, int startInclusive, int endExclusive) {
        return of(array, startInclusive, endExclusive).mapToLong(Long::longValue);
    }

    public static DoubleStream ofUnboxed(Double[] array) {
        return of(array).mapToDouble(Double::doubleValue);
    }

    public static DoubleStream ofUnboxed(Double[] array, int startInclusive, int endExclusive) {
        return of(array, startInclusive, endExclusive).mapToDouble(Double::doubleValue);
    }

}
posted @ 2022-08-25 15:43  XKIND  阅读(24)  评论(0编辑  收藏  举报