package lambda; import java.util.Comparator; import java.util.function.Consumer; import org.junit.Test; public class TestLambda2 { @Test public void test1() { /** * 匿名內部類 */ Runnable ra=new Runnable() { @Override public void run() { // TODO Auto-generated method stub System.out.println("測試111111111111111"); } }; ra.run(); /** * lambda表達式 */ Runnable r1=() ->System.out.println("測試2222222222222"); r1.run(); } /** * 只有一個參數小括號能夠省略不寫 */ @Test public void test3() { Consumer<String>con=x->System.out.println(x); con.accept("我在測試》》》》》》》》》》》》》》"); } /** * 語法4 lambda表達式中有多個參數,有返回值 */ @Test public void test4() { Comparator<Integer>com=(x,y)->{ System.out.println("測試多個參數》》》》》》》》》》》》》"); return Integer.compare(x, y); }; int m=com.compare(4, 5); System.out.println("m===="+m); } /** * 語法5 多個參數,有返回值,方法中只有一個表達式,大括號能夠不寫,return 也能夠省略不寫 */ @Test public void Test5() { System.out.println("測試5》》》》》》》》》》》》》"); Comparator<Integer> com=(x,y)-> Integer.compare(x, y); int msg=com.compare(3, 7); System.out.println("msg======"+msg); } }