欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Lambda

程序员文章站 2022-04-17 23:19:04
...

 

该语法糖在jvm脚本语言Groovy/Scale已有很好的支持,Java从8.0才开始支持该特性。

 

/**
 * 基础Lambda表达式
 *
 */
public static void testLambda() {
	String[] ss = { "hello", "world", "sun" };
	List<String> list = Arrays.asList(ss);

	// 接口作为回调函数,C: typdef int (*func)(int,int)
	AddService func = (int a, int b) -> (a + b);
	func.add(20, 21);
	// 多条语句用{}包围
	AddService func1 = (int a, int b) -> { System.out.println("count"); return (a + b); }; 
	func1.add(20, 21);
	
	// 1.遍历
	// forEach
	list.forEach((player) -> System.out.println(player));

	// ::
	list.forEach(System.out::println);

	// 2.排序
	Arrays.sort(ss, (String s1, String s2) -> (s1.compareTo(s2)));

	// 3.线程
	new Thread(() -> System.out.println(Thread.currentThread().getName() + ">Say hello")).start();
	Runnable th1 = () -> System.out.println(Thread.currentThread().getName() + ">Say world");
	th1.run(); 
}

/**
 * Stream lambda
 * 强大的filter条件过滤
 */
public static void testStream() {
	// Programmer
	List<Person> progm = new ArrayList<Person>() {
		{
			add(new Person("Jarrod", "Pace", "PHP programmer", "male", 34, 1550));
			add(new Person("Clarette", "Cicely", "PHP programmer", "female", 23, 1200));
			add(new Person("Victor", "Channing", "PHP programmer", "male", 32, 1600));
			add(new Person("Tori", "Sheryl", "PHP programmer", "female", 21, 1000));
			add(new Person("Osborne", "Shad", "PHP programmer", "male", 32, 1100));
			add(new Person("Rosalind", "Layla", "PHP programmer", "female", 25, 1300));
			add(new Person("Fraser", "Hewie", "PHP programmer", "male", 36, 1100));
			add(new Person("Quinn", "Tamara", "PHP programmer", "female", 21, 1000));
			add(new Person("Alvin", "Lance", "PHP programmer", "male", 38, 1600));
			add(new Person("Evonne", "Shari", "PHP programmer", "female", 40, 1800));
		}
	};

	// 定义filter
	Predicate<Person> ageFilter = (p) -> (p.getAge() > 21);  // age>21
	Predicate<Person> salaryFilter = (p) -> (p.getSalary() > 1000); // salary>1000
	Predicate<Person> genderFilter = (p) -> ("female".equals(p.getGender())); // femail
	
	// 过滤年龄大于21岁且月薪大于$1,000以上的2名女程序员
	progm.stream()
			.filter(ageFilter)
			.filter(salaryFilter)
			.filter(genderFilter)
			.limit(2) 	// 只取2条记录
			.forEach((p) -> System.out.printf("%s %s; ", p.getFirstname(), p.getLastname()));
	
	// 根据姓排序,将结果输出至list
	List<Person> sortprogm = progm
			.stream()
			.sorted( (p,p2) -> (p.getFirstname().compareTo(p2.getFirstname())))
			.limit(2)
			.collect( Collectors.toList() );
	
	// 过滤工资最低的程序员
	Person per = progm
			.stream()
			.min( (p,p2) -> (p.getSalary() - p2.getSalary()) )
			.get();
	
	/// map/reduce lambda表达式不可或缺两组函数
	// map --> 将姓添加至set
	Set<String> set = progm
			.stream()
			.map(Person::getFirstname)
			.collect( Collectors.toSet() );
	
	// reduce --> 所有工资的总和
	int total = progm
			.stream()
			.map( Person::getSalary )
			.reduce( 0, (a,b) -> a+b );
	
	// 数组最大最小值,总和,平均值等汇总
	List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);  
	IntSummaryStatistics stats = numbers  
			  .stream()  
			  .mapToInt((x) -> x)  
			  .summaryStatistics();
}

 

参考链接:

Java lambda表达式入门

 

 

 

 

 

 

 

 

 

 

 

 

相关标签: lambda map reduce