Lambda 表達式 (一)

標題

package com.dsk.list;

import java.util.Comparator;

import org.junit.Test;
interface B{
	public int add(int x,int y);
}
public class TestLambda  {
       @Test
      public void test(){
    	   // 無參數 無返回值  () 代替 new Runnable()  -> 右側 代替方法體
    	  Runnable r1=()->System.out.println("hello");
    	  //(x,y) 代替(int x,int y) ,->右側 是該方法具體 實現
    	  B b=(x,y)->{
    		 System.out.println(x); 
    		 System.out.println(y);
			return x+y;
    	  };
    	System.out.println( b.add(5, 5));
    	//(x,y) 代替 int compare(T o1, T o2);
    	Comparator<Integer> comparator=(x,y)->{
    		System.out.println("函數式編程接口");
			return Integer.compare(x, y);
    		
    	};
    	//調用方法
    	comparator.compare(5, 6);
      }
       
}
相關文章
相關標籤/搜索