赞
踩
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().forEach(item -> {
System.out.println(item);
});
System.out.println("==============");
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属性的集合
list.stream().forEach(item ->{
System.out.println(item);
});
======遍历=====
People{id=1, name='Ligs', sex=true}
People{id=2, name='Songzx', sex=false}
People{id=3, name='Jinzg', sex=true}
People{id=4, name='Liuzx', sex=false}
People{id=5, name='Hedx', sex=true}
People{id=6, name='Quansm', sex=false}
People{id=7, name='Liangsz', sex=true}
People{id=8, name='Chisz', sex=true}
==============
[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}]
[true, false, true, false, true, false, true, true]
People{id=1, name='Ligs', sex=true}
People{id=2, name='Songzx', sex=false}
People{id=3, name='Jinzg', sex=true}
People{id=4, name='Liuzx', sex=false}
People{id=5, name='Hedx', sex=true}
People{id=6, name='Quansm', sex=false}
People{id=7, name='Liangsz', sex=true}
People{id=8, name='Chisz', sex=true}
Process finished with exit code 0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。