How to Use Java 8 Stream API in Java Projects

1. Overview

In this post, let's use Stream API methods to demonstrate it's usage in real projects.

In typical enterprise Java web applications, we retrieve an object from the database and convert it into DTO and then send DTO as JSON back to Client applications.

2. Stream API Example

Let's take the same example, first let's create Customer entity class and corresponding CustomerDTO.
public class Customer {
 private int id;
  private String firstName;
  private String lastName;

  /**
   * Creates an instance of customer.
   */
  public Customer(final int id, final String firstName, final String lastName) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public int getId() {
    return id;
  }

  public void setId(final int id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(final String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(final String lastName) {
    this.lastName = lastName;
  }

}
public class CustomerDTO {
 private int id;
  private String firstName;
  private String lastName;

  /**
   * Creates an instance of customer.
   */
  public CustomerDTO(final int id, final String firstName, final String lastName) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public int getId() {
    return id;
  }

  public void setId(final int id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(final String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(final String lastName) {
    this.lastName = lastName;
  }

}
Let's write code to convert from Entity to DTO using Stream API methods.
The normal way to convert from Entity to DTO before Java 8.
private static Set<CustomerDTO> BeforeJava8Demo(){
 final Set<Customer> customers = new HashSet<>();
 customers.add(new Customer(100, "customer", "lastname1"));
 customers.add(new Customer(200, "customer2", "lastname2"));
 customers.add(new Customer(300, "customer3", "lastname3"));
 customers.add(new Customer(400, "customer4", "lastname4"));
 final Set<CustomerDTO> customerDTOs = new HashSet<>();
 for(Customer customer : customers){
  customerDTOs.add(new CustomerDTO(customer.getId(), customer.getFirstName(),customer.getLastName()));
 }
 
 for(CustomerDTO customerDTO : customerDTOs){
  System.out.println(customerDTO.getId());
 }
 return customerDTOs;
}
In Java 8, we can use Stream API 's map () and collect() methods.
private static Set<CustomerDTO> java8StreamAPIDemo(){
 final Set<Customer> customers = new HashSet<>();
 customers.add(new Customer(100, "customer", "lastname1"));
 customers.add(new Customer(200, "customer2", "lastname2"));
 customers.add(new Customer(300, "customer3", "lastname3"));
 customers.add(new Customer(400, "customer4", "lastname4"));
 
 final Set<CustomerDTO> customerDTOs = customers.stream().map(c ->{
  final CustomerDTO customerDTO = new CustomerDTO(c.getId(), c.getFirstName(), c.getLastName());
  return customerDTO;
 }).collect(Collectors.toSet());

 customerDTOs.forEach( c -> System.out.println(c.getId()));
 return customerDTOs;
}

3. Complete Example for Reference

public class StreamMapTest {
 public static void main(String[] args) {
  
  // Convert entity to DTO using Set
  final Set<Customer> customers = new HashSet<>();
  customers.add(new Customer(100, "customer", "lastname1"));
  customers.add(new Customer(200, "customer2", "lastname2"));
  customers.add(new Customer(300, "customer3", "lastname3"));
  customers.add(new Customer(400, "customer4", "lastname4"));
  
  final Set<CustomerDTO> customerDTOs = 
   customers.stream().map(c ->{
   final CustomerDTO customerDTO = 
   new CustomerDTO(c.getId(), c.getFirstName(), c.getLastName());
   return customerDTO;
  }).collect(Collectors.toSet());
 
  customerDTOs.forEach( c -> System.out.println(c.getId()));
  
  // Convert entity to DTO using List
  final List<Customer> list = new ArrayList<>();
  list.add(new Customer(100, "customer", "lastname1"));
  list.add(new Customer(200, "customer2", "lastname2"));
  list.add(new Customer(300, "customer3", "lastname3"));
  list.add(new Customer(400, "customer4", "lastname4"));
  
  final List<CustomerDTO> dtos = list.stream().map(c ->{
   final CustomerDTO customerDTO = 
   new CustomerDTO(c.getId(), c.getFirstName(), c.getLastName());
   return customerDTO;
  }).collect(Collectors.toList());
 
  dtos.forEach( c -> System.out.println(c.getId()));
 }
 
 
 private static Set<CustomerDTO> BeforeJava8Demo(){
  final Set<Customer> customers = new HashSet<>();
  customers.add(new Customer(100, "customer", "lastname1"));
  customers.add(new Customer(200, "customer2", "lastname2"));
  customers.add(new Customer(300, "customer3", "lastname3"));
  customers.add(new Customer(400, "customer4", "lastname4"));
  final Set<CustomerDTO> customerDTOs = new HashSet<>();
  for(Customer customer : customers){
   customerDTOs.add(new CustomerDTO(customer.getId(),
   customer.getFirstName(),customer.getLastName()));
  }
  
  for(CustomerDTO customerDTO : customerDTOs){
   System.out.println(customerDTO.getId());
  }
  return customerDTOs;
 }
 
 private static Set<CustomerDTO> java8StreamAPIDemo(){
  final Set<Customer> customers = new HashSet<>();
  customers.add(new Customer(100, "customer", "lastname1"));
  customers.add(new Customer(200, "customer2", "lastname2"));
  customers.add(new Customer(300, "customer3", "lastname3"));
  customers.add(new Customer(400, "customer4", "lastname4"));
  
  final Set<CustomerDTO> customerDTOs = customers.stream().map(c ->{
   final CustomerDTO customerDTO = new CustomerDTO(c.getId(),
   c.getFirstName(), c.getLastName());
   return customerDTO;
  }).collect(Collectors.toSet());
 
  customerDTOs.forEach( c -> System.out.println(c.getId()));
  return customerDTOs;
 }
}

4. Conclusion

In this post, we learned how to use Stream API with a simple Entity converter example.
You may like to read: Handle NullPointerException in Controller, Service and DAO Layer using Java 8 Optional Class.


Comments

Post a Comment