import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName Demo
* @Description Lambda表达式--List过滤指定元素
* @author TianJianWen
* @data 2019年11月14日下午3:27:26
*
*/
public interface Demo_List {
public static void main(String[] args) {
List<People> list = new ArrayList<>();
list.add(new People("zs", 22));
list.add(new People("ls", 27));
list.add(new People("ww", 29));
list.add(new People("zl", 21));
// 集合遍历查找 年龄在20-25岁的人
List<People> search = new ArrayList<>();
// 1.传统方式
for (People people : list) {
if (people.getAge() >= 20 && people.getAge() <= 25){
search.add(people);
}
}
// 2.Lambda使用
// Stream是Java 8 提供的高效操作集合类(Collection)数据的API。
search = list.stream().filter((People people) -> people.getAge() >= 20 && people.getAge() <= 25).collect(Collectors.toList());
// 打印结果
for (People people : search) {
System.out.println(people.getName() + "-" + people.getAge());
}
}
}
class People {
public People(String name, Integer age) {
this.name = name;
this.age = age;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}