更新时间:2022-02-17 02:39:48
您必须有一些方法/构造函数来生成现有 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
放入一个新的列表
:
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());