如何在Java项目中使用Java 8 Stream API

x33g5p2x  于4个月前 转载在 Java  
字(5.5k)|赞(0)|评价(0)|浏览(84)

1.概述

在本文中,让我们使用Stream API方法来演示它在实际项目中的用法。
在典型的企业Java web applications中,我们从数据库中检索一个对象,并将其转换为DTO,然后将DTO作为JSON发送回客户端应用程序。

2.流API示例

让我们以相同的示例为例,首先创建Customer实体类和相应的CustomerDTO.

public class Customer {
 private int id;
  private String firstName;
  private String lastName;

  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;
  }

}

让我们编写代码,使用Stream API方法将Entity转换为DTO
在Java 8之前,将Entity转换为DTO的常规方法。

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;
}

在Java8中,我们可以使用Stream API的**map()collect()**方法。

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.完整示例供参考

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.结论

在本文中,我们通过一个简单的实体转换器示例学习了如何使用Stream API。

相关文章

微信公众号

最新文章

更多