在本文中,让我们使用Stream API
方法来演示它在实际项目中的用法。
在典型的企业Java web applications
中,我们从数据库中检索一个对象,并将其转换为DTO
,然后将DTO作为JSON发送回客户端应用程序。
让我们以相同的示例为例,首先创建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;
}
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;
}
}
在本文中,我们通过一个简单的实体转换器示例学习了如何使用Stream API。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/07/how-to-use-java-8-stream-api-in-java-projects.html
内容来源于网络,如有侵权,请联系作者删除!