Commonly used Stream api methods

Stream api is one of the major changes introduced in Java 8.

why do we use "stream api" is it the same as Collection API?

The answer is no, collection classes store elements, whereas stream API does not store, it is used to process a group of elements. Below are a few examples to create streams,

Ex 1: List marks = List.of(1,2,6,4,3,5);

Stream stream = marks.stream();(where marks can be any collection object)

Ex 2: Integer[] intArray = {5,10,15,20,25};

Stream stream = Stream.of(intArray);

Ex 3: Stream stream = Stream.of(30,35,40,45,50);
stream.forEach(System.out::println);

Stream api methods are lazy, they perform the computation of the stream only when the terminal operation is initialized. And also stream API methods consume source input elements as needed.

filter(): * filter method used to filter the objects based on some condition.
* filter() accepts Predicate as a condition and applies it on the stream resultant is also stream. Predicate is a functional interface that returns true/false.
Ex: Integer[] intArray = {5,10,15,20,25};
List evenList = Stream.of(intArray).filter(p -> p%2==0).collect(Collectors.toList());

above example creates a list of even numbers from the intArray.

map() : * map method converts the stream based on the function provided as input and returns the resultant stream.

<R> Stream<R> map(Function<? super T, ? extends R> mapper)
<R> type of resultant stream, the input stream is of type T.

EX: multiply each element in a list by 5
List list = List.of(5,10,15,20,25);
List newList = list.stream().map(s -> s*5).collect(Collectors.toList());

sorted(): *to sort elements of the stream.
* two variants of sorted are available in stream API.
\> sorted() which does not take any input, and sorts elements in natural sorting order.
\> sorted(Comparator< super T>comparator), which enables the user to provide a customized sorting mechanism.
EX:
List marks = List.of(57,43,89,32,25); List naturalSort = marks.stream().sorted().toList();

below is the example for customized sorting to sort in descending order by implementing compare method of the Comparator functional interface with the help of lambda expression

List marks = List.of(57,43,89,32,25); List sorted =marks.stream().sorted((s1,s2) -> s1<s2? 1: s1>s2?-1:0).collect(Collectors.toList());

filter, map and sorted are intermediate methods of Stream API. These transform a stream to another stream based on the condition provided.
collect(): * one of the terminal methods of Stream API.
* collector method used to perform mutable reduction operations on the elements of stream. There are two variants of collect method in Stream API.
* In the above examples collect method is used to repackage a stream of elements using the toList method of the Collectors class.
count(): * terminal method of Stream Api.
* used to count the no of elements in the source stream.
Ex: count no of whose employees salary < 25000.
Employee has two properties name and salary.

long noOfEmp = list.stream().filter(s -> s.getSalary() < 25000).count();
filter method used to calculate no. of employees whose salary is less than 25k.

forEach(): * forEach() is a terminal method
* forEach method accepts Consumer implementation, processes each element of the stream.
* forEach can be used as a replacement for Iterator and for loop
Ex: In the above employee list increase the salary of employee by 80% whose salary is less than 25k and write them to console.
List empList = list.stream().filter(s -> s.getSalary() > 25000).map(s -> { Employee t = new Employee(); t.setName(s.getName()); t.setSalary((int) (s.getSalary() + (s.getSalary() * 0.8))); return t; }).collect(Collectors.toList());

empList.stream().forEach(System.out::println);

max() and min(): * both perform terminal operation.
* finds max/min element in the stream based on the input Comparator sorting mechanism.
min and max in case of natural sorting,
Ex: List marks = List.of(57,43,89,32,25); Integer min = marks.stream().min((s1,s2) -> s1.compareTo(s2)).get(); Integer max = marks.stream().max((s1,s2) -> s1.compareTo(s2)).get();

output of above snippet : min 25 max 89

min and max incase of descending order,
Ex: List marks = List.of(57,43,89,32,25); Integer min = marks.stream().min((s1,s2) -> -s1.compareTo(s2)).get(); Integer max = marks.stream().max((s1,s2) -> -s1.compareTo(s2)).get();

result of the above example min 89 and max 25