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

x33g5p2x  于2022-10-14 转载在 Java  
字(5.5k)|赞(0)|评价(0)|浏览(886)

1.概述

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

2.流API示例

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

  1. public class Customer {
  2. private int id;
  3. private String firstName;
  4. private String lastName;
  5. public Customer(final int id, final String firstName, final String lastName) {
  6. this.id = id;
  7. this.firstName = firstName;
  8. this.lastName = lastName;
  9. }
  10. public int getId() {
  11. return id;
  12. }
  13. public void setId(final int id) {
  14. this.id = id;
  15. }
  16. public String getFirstName() {
  17. return firstName;
  18. }
  19. public void setFirstName(final String firstName) {
  20. this.firstName = firstName;
  21. }
  22. public String getLastName() {
  23. return lastName;
  24. }
  25. public void setLastName(final String lastName) {
  26. this.lastName = lastName;
  27. }
  28. }
  29. public class CustomerDTO {
  30. private int id;
  31. private String firstName;
  32. private String lastName;
  33. /**
  34. * Creates an instance of customer.
  35. */
  36. public CustomerDTO(final int id, final String firstName, final String lastName) {
  37. this.id = id;
  38. this.firstName = firstName;
  39. this.lastName = lastName;
  40. }
  41. public int getId() {
  42. return id;
  43. }
  44. public void setId(final int id) {
  45. this.id = id;
  46. }
  47. public String getFirstName() {
  48. return firstName;
  49. }
  50. public void setFirstName(final String firstName) {
  51. this.firstName = firstName;
  52. }
  53. public String getLastName() {
  54. return lastName;
  55. }
  56. public void setLastName(final String lastName) {
  57. this.lastName = lastName;
  58. }
  59. }

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

  1. private static Set<CustomerDTO> BeforeJava8Demo(){
  2. final Set<Customer> customers = new HashSet<>();
  3. customers.add(new Customer(100, "customer", "lastname1"));
  4. customers.add(new Customer(200, "customer2", "lastname2"));
  5. customers.add(new Customer(300, "customer3", "lastname3"));
  6. customers.add(new Customer(400, "customer4", "lastname4"));
  7. final Set<CustomerDTO> customerDTOs = new HashSet<>();
  8. for(Customer customer : customers){
  9. customerDTOs.add(new CustomerDTO(customer.getId(), customer.getFirstName(),customer.getLastName()));
  10. }
  11. for(CustomerDTO customerDTO : customerDTOs){
  12. System.out.println(customerDTO.getId());
  13. }
  14. return customerDTOs;
  15. }

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

  1. private static Set<CustomerDTO> java8StreamAPIDemo(){
  2. final Set<Customer> customers = new HashSet<>();
  3. customers.add(new Customer(100, "customer", "lastname1"));
  4. customers.add(new Customer(200, "customer2", "lastname2"));
  5. customers.add(new Customer(300, "customer3", "lastname3"));
  6. customers.add(new Customer(400, "customer4", "lastname4"));
  7. final Set<CustomerDTO> customerDTOs = customers.stream().map(c ->{
  8. final CustomerDTO customerDTO = new CustomerDTO(c.getId(), c.getFirstName(), c.getLastName());
  9. return customerDTO;
  10. }).collect(Collectors.toSet());
  11. customerDTOs.forEach( c -> System.out.println(c.getId()));
  12. return customerDTOs;
  13. }

3.完整示例供参考

  1. public class StreamMapTest {
  2. public static void main(String[] args) {
  3. // Convert entity to DTO using Set
  4. final Set<Customer> customers = new HashSet<>();
  5. customers.add(new Customer(100, "customer", "lastname1"));
  6. customers.add(new Customer(200, "customer2", "lastname2"));
  7. customers.add(new Customer(300, "customer3", "lastname3"));
  8. customers.add(new Customer(400, "customer4", "lastname4"));
  9. final Set<CustomerDTO> customerDTOs =
  10. customers.stream().map(c ->{
  11. final CustomerDTO customerDTO =
  12. new CustomerDTO(c.getId(), c.getFirstName(), c.getLastName());
  13. return customerDTO;
  14. }).collect(Collectors.toSet());
  15. customerDTOs.forEach( c -> System.out.println(c.getId()));
  16. // Convert entity to DTO using List
  17. final List<Customer> list = new ArrayList<>();
  18. list.add(new Customer(100, "customer", "lastname1"));
  19. list.add(new Customer(200, "customer2", "lastname2"));
  20. list.add(new Customer(300, "customer3", "lastname3"));
  21. list.add(new Customer(400, "customer4", "lastname4"));
  22. final List<CustomerDTO> dtos = list.stream().map(c ->{
  23. final CustomerDTO customerDTO =
  24. new CustomerDTO(c.getId(), c.getFirstName(), c.getLastName());
  25. return customerDTO;
  26. }).collect(Collectors.toList());
  27. dtos.forEach( c -> System.out.println(c.getId()));
  28. }
  29. private static Set<CustomerDTO> BeforeJava8Demo(){
  30. final Set<Customer> customers = new HashSet<>();
  31. customers.add(new Customer(100, "customer", "lastname1"));
  32. customers.add(new Customer(200, "customer2", "lastname2"));
  33. customers.add(new Customer(300, "customer3", "lastname3"));
  34. customers.add(new Customer(400, "customer4", "lastname4"));
  35. final Set<CustomerDTO> customerDTOs = new HashSet<>();
  36. for(Customer customer : customers){
  37. customerDTOs.add(new CustomerDTO(customer.getId(),
  38. customer.getFirstName(),customer.getLastName()));
  39. }
  40. for(CustomerDTO customerDTO : customerDTOs){
  41. System.out.println(customerDTO.getId());
  42. }
  43. return customerDTOs;
  44. }
  45. private static Set<CustomerDTO> java8StreamAPIDemo(){
  46. final Set<Customer> customers = new HashSet<>();
  47. customers.add(new Customer(100, "customer", "lastname1"));
  48. customers.add(new Customer(200, "customer2", "lastname2"));
  49. customers.add(new Customer(300, "customer3", "lastname3"));
  50. customers.add(new Customer(400, "customer4", "lastname4"));
  51. final Set<CustomerDTO> customerDTOs = customers.stream().map(c ->{
  52. final CustomerDTO customerDTO = new CustomerDTO(c.getId(),
  53. c.getFirstName(), c.getLastName());
  54. return customerDTO;
  55. }).collect(Collectors.toSet());
  56. customerDTOs.forEach( c -> System.out.println(c.getId()));
  57. return customerDTOs;
  58. }
  59. }

4.结论

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

相关文章

最新文章

更多