1. 程式人生 > 其它 >lambda表示式求和和排序

lambda表示式求和和排序

package lambda;

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

import org.junit.Test;

public class TestFunction {
	/**
	 * lambda表示式計算求和
	 */
	@Test
	public void Test() {
		MyFunction mf=(x)->++x;
		int m=mf.getAdd(3);
		System.out.println("m===="+m);
	}
	/**
	 * lambda表示式計算求和
	 */
	
	@Test
	public void Test2() {
		int sum=getSum(2,(e)->e+=1);
		System.out.println("sum==="+sum);
		
	}
	/**
	 * 計算方法
	 * @param m
	 * @param mf
	 * @return
	 */
	private Integer getSum(int m,MyFunction mf) {
		return mf.getAdd(m);
	}
	/**
	 * 按年齡和名字排序
	 */
	@Test
	public void test3() {
		List<Employee>list=new ArrayList<Employee>();
		list.add(new Employee(12,"張三",500));
		list.add(new Employee(11,"李四",500));
		list.add(new Employee(110,"王五",500));
		list.add(new Employee(15,"趙琦",500));
		Collections.sort(list,(x,y)->{
			if(x.getAge()==y.getAge()) {
				return x.getName().compareTo(y.getName());
			}else {
				return Integer.compare(x.getAge(), y.getAge());
			}
		});
		
		for(Employee em:list) {
			System.out.println("em==="+em);
		}
	}

}