且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 8修改流元素

更新时间:2022-02-17 02:39:36

您必须具有一些生成现有SampleDTO实例副本的方法/构造函数,例如副本构造函数.

You must have some method/constructor that generates a copy of an existing SampleDTO instance, such as a copy constructor.

然后,您可以将每个原始SampleDTO实例map变为一个新的SampleDTO实例,并将它们collect变为一个新的List:

Then you can map each original SampleDTO instance to a new SampleDTO instance, and collect them into a new List :

List<SampleDTO> output = 
    list.stream()
        .map(s-> {
                     SampleDTO n = new SampleDTO(s); // create new instance
                     n.setText(n.getText()+"xxx"); // mutate its state
                     return n; // return mutated instance
                 })
       .collect(Collectors.toList());