Streams are one of the major features introduced in Java 8. Streams are a sequence of elements that can be easily obtained from a Collection
. Streams should not be confused with File I/O streams which are entirely different.
Collections
, Arrays
, or I/O channels
.These are operations that result in other streams which means more processing can still be done on them, like:
map()
method converts Stream<X>
to Stream<Y>
. For each object of type X
a new object of type Y
is created and put in the new Stream
.List number = Arrays.asList(2,3,4,5);
List square = number.stream().map(x->x*x).collect(Collectors.toList());
filter()
method is used to select elements that match the predicate that is passed as an argument.List names = Arrays.asList("Reflection", "Collection", "Stream");
List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());
sorted()
method is used to sort the stream.List names = Arrays.asList("Reflection", "Collection", "Stream");
List result = names.stream().sorted().collect(Collectors.toList());
Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined.
These are operations that result in a final value that marks the stream as consumed and no further processing can be done.
collect()
method is used to receive elements from a stream and store them in a collection.List number = Arrays.asList(2,3,4,5,3);
Set square = number.stream().map(x->x*x).collect(Collectors.toSet());
forEach()
method helps iterate over all stream elements and perform some operation on each of them. The operation to be performed is passed as the lambda expression.List number = Arrays.asList(2,3,4,5);
number.stream().map(x->x*x).forEach(y->System.out.println(y));
reduce()
method performs a reduction on the elements of the stream with the given function. The result is an Optional holding the reduced value.List number = Arrays.asList(2,3,4,5);
int even = number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans+i);