大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
在看代码的时候看到了相关方法,自己在写了个例子练习一下
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属性的集合
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/230607.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...