Guide to Java Stream API: Basic Operations
Now let’s see some common usages and operations that we can perform on Java and with the help of stream support.
Intermediate Operations
The stream itself is returned by intermediate tasks, so that several method calls can be chained in a sequence. Let’s learn important ones.
Filter
The filter
method accepts a Predicate to filter all elements of the stream. This operation is an intermediate operation which allows one to call the result to another stream operation (e.g.foreach
).
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");
list.stream()
.filter((s) -> s.startsWith("a"))
.forEach(System.out::println);//a2
//a1
Map
The map
operation converts each element in the stream into another object via the given function.
The following example converts each string into an UPPERCASE string. But we can use map
to transform an object into another type.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");
list.stream()
.filter((s) -> s.startsWith("a"))
.map(String::toUpperCase)
.forEach(System.out::println);//A2
//A1
Sorted
The sorted
method returns a sorted view of the stream. The elements in the stream are sorted in natural order unless we pass a custom Comparator.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");
list.stream()
.sorted()
.map(String::toUpperCase)
.forEach(System.out::println);//A1
//A2
//B1
//B3
//C2
Short-circuit Intermediate Operations
Just like intermediate operations but it is often desired to break the operation whenever a matching element is encountered during iteration.
AnyMatch
The anyMatch
will return true
once a condition passed. Once a matching value is found, no more elements will be processed in the stream.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");
boolean answer = list.stream()
.anyMatch((s) -> s.startsWith("c"));
System.out.println(answer);//true
FindFirst
The findFirst
method will return the first element from the stream and then it will not process any more elements.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");
boolean answer = list.stream()
.filter((s) -> s.startsWith("b"))
.findFirst().isPresent();
System.out.println(answer);//true
Terminal operations
Terminal operations return a result of a certain type after processing all the stream elements.
Once the terminal operation is invoked on a Stream, the iteration of the Stream and any of the chained streams will get started.
ForEach
The forEach
method helps in iterating over all elements of a stream and perform some operation on each of them.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");
list.stream()
.forEach(System.out::println);//a2
//a1
//b1
//b3
//c2
The operation to be performed is passed as the lambda expression.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");
list.stream()
.forEach(x -> System.out.println("element: " + x));//element: a2
//element: a1
//element: b1
//element: b3
//element: c2
Collect
The collect
method is used to receive elements from steam and store them in a collection.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");List<String> selectedList = list.stream()
.filter((s) -> s.startsWith("a"))
.map(String::toUpperCase)
.collect(Collectors.toList());
selectedList.stream()
.forEach(System.out::println);//A2
//A1
Count
The count
method is an operation returning the number of elements in the stream as a long
value.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");long totalMatched = list.stream()
.filter((s) -> s.startsWith("a"))
.count();System.out.println(totalMatched);
//2
Reduce
The reduction operation combines all elements of the stream into a single result.
List<String> list = Arrays.asList("a2", "a1", "b1", "b3", "c2");Optional<String> reduced = list.stream()
.reduce((s1, s2) -> s1 + "-" + s2);
reduced.ifPresent(System.out::println);//a2-a1-b1-b3-c2
In my next article, I will talk about stream execution orders.