Sitemap
Javarevisited

A humble place to learn Java and Programming better.

Java 8 Stream API with Examples

--

Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

The features of the Java stream are –

  • A stream is not a data structure instead it takes input from the Collections, Arrays, or I/O channels.
  • Streams don’t change the original data structure, they only provide the result as per the pipelined methods.

In this article, we will explore how we can use the with some examples.

We need a list of objects to work with the stream API, we are going to make a list of employees for our example.

Create Employee Class

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;




public class Employee {

private String empId;
private String firstName;
private String lastName;
private String email;
private String gender;
private String newJoiner;
private int salary;
private int rating;

}

Create a Main class with and a static method that return a list of Employees

import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
}public static List<Employee> getEmpList(){
return Arrays.asList(
new Employee("59-385-1088","Zacharias","Schwerin","[email protected]","Male","True",101146,0),
new Employee("73-274-6476","Kyle","Frudd","[email protected]","Male","FALSE",29310,2),
new Employee("85-939-9584","Axe","Gumb","[email protected]","Female","FALSE",62291,4),
new Employee("08-180-8292","Robinet","Batterham","[email protected]","Male","FALSE",142439,4),
new Employee("21-825-2623","Ulick","Burrel","[email protected]","Male","FALSE",128764,5),
new Employee("66-708-5539","Tailor","Ridding","Ridding","Female","FALSE",152924,4),
new Employee("81-697-2363","Joete","Braybrooke","[email protected]","Male","TRUE",128907,0),
new Employee("63-019-1110","Elroy","Baverstock","[email protected]","Male","TRUE",2510,0)
);
}
}

We have a list of employees with empId, firstName, lastName, email, gender, newJoiner, salary and rating.

Filter Method

We will the employees list with gender as Female

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream().filter(e -> e.getGender().equals("Female")).forEach(e -> System.out.println(e));
}

On Execution:

We will filter the employees list with newJoiner as True

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream().filter(e -> e.getNewJoiner().equals("True")).forEach(e -> System.out.println(e));
}

On Execution:

Sort Method

We will the employee list by rating asc.

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream()
.sorted(Comparator.comparing(Employee::getRating))
.forEach(e -> System.out.println(e));
}

On Execution:

We will sort the employee list by rating desc.

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream()
.sorted(Comparator.comparing(Employee::getRating).reversed())
.forEach(e -> System.out.println(e));
}

On Execution:

We will

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();

empList.stream()
.sorted(Comparator.comparing(Employee::getSalary))
.sorted(Comparator.comparing(Employee::getRating))
.forEach(e -> System.out.println(e));
}

On Execution:

Match Method

We will see all employees with salary more than 1000


public static void main(String[] args) {

List<Employee> empList = getEmpList();
boolean isSalary = empList.stream()
.allMatch(e -> e.getSalary() > 1000);
System.out.println(isSalary);
}

On Execution:

Max Function

We will retrieve employee with max salary

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream()
.max(Comparator.comparing(Employee::getSalary))
.ifPresent(System.out::println);
}

On Execution:

We will retrieve employee with max rating

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream()
.max(Comparator.comparing(Employee::getRating))
.ifPresent(System.out::println);
}

On Execution:

Min Function

We will retrieve employee with min salary


public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream()
.min(Comparator.comparing(Employee::getSalary))
.ifPresent(System.out::println);
}

On Execution:

We will retrieve employee with min rating

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
empList.stream()
.min(Comparator.comparing(Employee::getRating))
.ifPresent(System.out::println);
}

On Execution:

GroupBy Function

We will all our employees by Gender

public class Main {
public static void main(String[] args) {

List<Employee> empList = getEmpList();
Map<String, List<Employee>> employeesBygender = empList.stream()
.collect(Collectors.groupingBy(Employee::getGender));
employeesBygender.forEach(((g,e)->{
System.out.println(g);
e.forEach(System.out::println);
}));
}

On Execution:

Conclusion

We have gone through a few simple examples and If you’re interested in learning more about Java 8 streams, I recommend viewing the offical Stream Javadoc package documentation.

Hopefully this tutorial was helpful to you and you’ve enjoyed reading it.

Happy coding..

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Thameem Ansari
Thameem Ansari

Written by Thameem Ansari

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps|

Responses (1)