在看代码的时候看到了相关方法,自己在写了个例子练习一下
public class People {
private Integer id;
private String name;
private Boolean sex;
//省略
}
public class Filter {
public static void main(String[] args) {
People people1 = new People(1, "Ligs", true);
People people2 = new People(2, "Songzx", false);
People people3 = new People(3, "Jinzg", true);
People people4 = new People(4, "Liuzx", false);
People people5 = new People(5, "Hedx", true);
People people6 = new People(6, "Quansm", false);
People people7 = new People(7, "Liangsz", true);
People people8 = new People(8, "Chisz", true);
ArrayList<People> list = new ArrayList<People>() {
{
add(people1);
add(people2);
add(people3);
add(people4);
add(people5);
add(people6);
add(people7);
add(people8);
}};
System.out.println(list.stream().filter(People::getSex).collect(Collectors.toList()));
//[People{id=1, name='Ligs', sex=true}, People{id=3, name='Jinzg', sex=true}, People{id=5, name='Hedx', sex=true}, People{id=7, name='Liangsz', sex=true}, People{id=8, name='Chisz', sex=true}]
//过滤掉sex为false值的对象
System.out.println(list.stream().map(People::getSex).collect(Collectors.toList()));
//[true, false, true, false, true, false, true, true]
//获取list中sex属性的集合
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/230607.html原文链接:https://javaforall.cn