• 【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);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
  • 相关阅读:
    死磕面试系列,Java到底是值传递还是引用传递?
    Java项目:ssm毕业论文管理系统
    报数模拟(二)
    基于Python实现Midjourney集成到(个人/公司)平台中
    文献阅读常用工具
    HTML 标签简写及全称
    MySQL高级4-索引的使用规则
    [软件工程] UML类图
    oracle创建表空间及查看表空间和使用情况
    运输层总结(未完待续)
  • 原文地址:https://blog.csdn.net/zte1055889498/article/details/126525332