List<Person> list = Arrays.asList(
new Person("张三1","男",14,"大连4"),
new Person("汪峰2","女",14,"大连3"),
new Person("谢娜3","男",15,"大连2"),
new Person("张杰4","男",17,"大连5"),
new Person("章子怡5","女",19,"大连1"),
new Person("袁咏仪6","男",18,"沈阳")
);
/**
* Stream API
*/
@Test
public void test1() {
List<Person> list1 = new ArrayList<Person>();
list.stream()
.filter((e) -> e.getAge() <= 15)
.forEach((p) -> list1.add(p));
list1.forEach(System.out::println);
System.out.println("-------------------------------------");
list.stream()
.map(Person::getAge)//变成map 曲年级
.limit(6)//跟数据库中是一样的,去前六个
.sorted(Comparator.reverseOrder())//排序
.filter((p) -> p < 15)//过滤
.forEach(System.out::println);//循环输出
}
/**
* Lambda 表达式;
* 先定义接口,然后在定义方法,最后调用方法
*/
@Test
public void test2() {
List<Person> listResult = returnList(list,(p) -> p.getAge() < 15);
listResult.forEach(System.out::println);
}
public List<Person> returnList(List<Person> paramList, Day1Test<Person> in) {
List<Person> newList = new ArrayList<Person>();
for(Person p:paramList) {
if(in.test(p)) {
newList.add(p);
}
}
return newList;
}