目录
- Stream改变list中特定对象的某一属性
- 看代码吧
- 总结
Stream改变list中特定对象的某一属性
list.stream() .filter(dog -> dog.getAge()<50) .forEach(dog -> dog.setAge(26));
看代码吧
import Java.util.ArrayList; import java.util.List; public class StreamTest { public static void main(String[] args) { Dog dog1=new Dog("旺财",13); Dog dog2=new Dog("旺财",45); Dog dog3=new Dog("编程客栈旺财",60); List<Dog> list=new ArrayList<>(); list.add(dog1); list.add(dog2); list.add(dog3); list.stream() .filter(dog -> dog.getAge()<50) .forEach(dog -> dog.setAge(26)); Systewww.devze.comm.oupythont.println(list); } } class Dog{ private String name; private Integer age; public Dog(String name, Integer age) { this.name = name; this.age = age; } public S编程tri编程客栈ng getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论