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); } } }