Create list of object from another using Java 8 Streams

后端 未结 3 1652
星月不相逢
星月不相逢 2020-11-27 15:36

I\'m trying to understand Java 8 streams. I have two classes:

public class UserMeal {
    protected final LocalDateTime dateTime;

    protected final String         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 16:25

    An addition to the solution by @Rafael Teles. The syntactic sugar Collectors.mapping does the same in one step:

    //...
    List employees = persons.stream()
      .filter(p -> p.getLastName().equals("l1"))
      .collect(
        Collectors.mapping(
          p -> new Employee(p.getName(), p.getLastName(), 1000),
          Collectors.toList()));
    

    Detailed example can be found here

提交回复
热议问题