-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamApiExample.java
101 lines (84 loc) · 2.09 KB
/
StreamApiExample.java
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
package DataStructures;
/**
* @author Srinvas Vadige, [email protected]
* @since 21 Sept 2024
*/
public class StreamApiExample {
public static void main(String[] args) {
}
}
/*
STREAM METHODS
===============
Stream can only be used once. We cannot re-use stream variable second time.
STREAM CONVERSION
————————————-
IntStream.of(1,2,3) or IntStream(int[])
Arrays.stream(arr)
Stream.of(ele1, ele2 …..) or Stream.of(arr or lst)
list.stream() or map.stream() or set.stream()
METHODS
—————-
Stream.concat(Stream1, Stream2)
.of(ele) or .of(eles) or .of(arr or lst)
.filter(Predicate)
.allMatch(Predicate)
.anyMatch(Predicate)
.noneMatch(Predicate)
.peek(Consumer)
.forEach(Consumer)
.forEachOrdered(Consumer)
.findAny()
.findFirst()
.count() - size
.distinct()
.empty()
.limit(intMaxSize)
.skip(longVal)
.min(Comparator)
.max(Comparator)
.sum() or collect(Collectors.summingInt(Integer::intValue))
.reduce(BinaryOperator accumulator)
.reduce(T identity, BinaryOperator)
.reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
.sorted()
.sorted(Comparator)
.builder()
.peek(Consumer)
.toArray()
.toArray(IntFunction<A[]> generator)
.map(Function)
.mapToInt()
.mapToDouble(ToDoubleFunction)
.mapToLong(ToLongFunction)
.flatMap(Function<T, Stream> mapper)
.flatMapToInt(Function<T, IntStream>)
.flatMapToDouble(Function<T, DoubleStream>)
.flatMapToLong(Function<T, LongStream>)
.collect(Collector)
.collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner
.boxed() —- to arrays int stream to Integer Stream
Java 9 methods
.ofNullable(ele)
.takeWhile(Predicate)
.dropWhile(Predicate)
.iterate(seed, UnaryOperator)
.iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
Java 16 methods
.mapMulti()
.mapMultiToInt()
.mapMultiToDouble()
.mapMultiToLong()
.toList() — to immutable list
Java 22 methods
.gather(GathererInPreview)
Methods inherited from interface java.util.stream.BaseStream
.close()
.onClose()
.isParallel()
.parallel()
.iterator()
.spliterator()
.sequential()
.unordered()
*/