lambba表达式

package test;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class MyInfo {
	@Test
	public void test2() {
		System.out.println("测试===============");
		
	}
	/**
	 * 使用lambda表达式
	 */
	@Test
	public void test1() {
		List<Person>list=new ArrayList<Person>();
		list.add(new Person("张三",30,500));
		list.add(new Person("张4",20,600));
		list.add(new Person("张5",40,300));
		list.add(new Person("张6",50,200));
		list.add(new Person("张7",35,700));
		System.out.println("list====="+list);
		List<Person>personList=getPersonList(list,(e) -> e.getSal()>=500);
		System.out.println("personList======"+personList);
	}
   
	public List<Person>getPersonList(List<Person>list,MyPrivate<Person>mp){
		List<Person>personList=new ArrayList<Person>();
		for(Person person:list) {
			if(mp.getInfo(person)) {
				personList.add(person);
			}
		}
		return personList;
	}
	
	/**
	 * 优化方式4
	 */
	@Test
	public void Test4() {
		List<Person>list=new ArrayList<Person>();
		list.add(new Person("张三",30,500));
		list.add(new Person("张4",20,600));
		list.add(new Person("张5",40,300));
		list.add(new Person("张6",50,200));
		list.add(new Person("张7",35,700));
		System.out.println("list4====="+list);
		list.stream().filter((e)->e.getAge()>=35).forEach(System.out::println);
		System.out.println("---------------------------");
		list.stream().map(Person::getName).forEach(System.out::println);
		
		
	}
}

  

原文地址:https://www.cnblogs.com/xianz666/p/14999020.html