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 stream API 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;@Data
@NoArgsConstructor
@AllArgsConstructor
@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 public static void main(String[] args){} function 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","zchwerin@gmail.com","Male","True",101146,0),
new Employee("73-274-6476","Kyle","Frudd","kfrudd1@ovh.net","Male","FALSE",29310,2),
new Employee("85-939-9584","Axe","Gumb","agumb2@twitter.com","Female","FALSE",62291,4),
new Employee("08-180-8292","Robinet","Batterham","rbatterham3@last.fm","Male","FALSE",142439,4),
new Employee("21-825-2623","Ulick","Burrel","uburrel4@google.ru","Male","FALSE",128764,5),
new Employee("66-708-5539","Tailor","Ridding","Ridding","Female","FALSE",152924,4),
new Employee("81-697-2363","Joete","Braybrooke","jbraybrooke6@prnewswire.com","Male","TRUE",128907,0),
new Employee("63-019-1110","Elroy","Baverstock","ebaverstock7@ehow.com","Male","TRUE",2510,0)
);
}
}
We have a list of employees with empId, firstName, lastName, email, gender, newJoiner, salary and rating.
Filter Method
We will filter 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 sort 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 sort the employee list by both rating and salary
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 group 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..