1. 程式人生 > >Lambda表示式--Java8的新功能案例詳解(2) Lambda表示式的方法引用

Lambda表示式--Java8的新功能案例詳解(2) Lambda表示式的方法引用

Lambda表示式與內部類相比有很多限制,比如只能為函式式介面建立例項,但是Lambda表示式大大簡化了程式碼的書寫。

Lambda表示式的方法引用主要分為下面幾類:

1.引用類方法
2.引用特定物件的例項方法
3.引用某類物件的例項方法
4.引用構造方法

下面通過幾個例項演示了這幾種方法引用的:

1.首先建立下面的介面

/*
該函式式介面只包含一個抽象方法convert——用於將String型別的引數轉換為Intege型別的返回值
*/
@FunctionalInterface
interface Converter
{
	Integer convert(String from);
}

/*
函式式介面,根據3個引數產生一個String型別的返回值
*/
@FunctionalInterface
interface MakeString
{
	String make(String a,int b,int c);
}

/*
函式式介面,根據輸入的title返回一個JFrame物件
*/
@FunctionalInterface
interface GetJFrame
{
	javax.swing.JFrame get(String title);
}

2.
/*
Lambda表示式的方法引用
1.引用類方法
2.引用特定物件的例項方法
3.引用某類物件的例項方法
4.引用構造方法
5.使用Lambda表示式呼叫Arrays類的方法

*/
public class MethodRefer
{
	public static void main(String[] args)
	{
	//-------------------------------------------1.引用類方法-----------------------------------------------
	    /*
		使用Lambda表示式建立Converter物件
		由於ambda表示式只有一個引數、一條語句和一個返回值,
		因此可以省略引數的小括號、語句的花括號和返回值的return關鍵字
		*/
		Converter con_1=from->Integer.valueOf(from);
		/*
		方法引用代替Lambda表示式:引用類方法
		函式式介面中被實現方法的全部引數傳給該類方法作為引數
		*/
		Converter con_2=Integer::valueOf;
		
		Integer val1=con_1.convert("111111");
		Integer val2=con_2.convert("222222");
		System.out.println(val1);
		System.out.println(val2);
	//---------------------------------------2.引用特定物件的例項方法-----------------------------------------
	   //呼叫String類的indexOf方法建立物件
	    Converter con_4="sharejava"::indexOf;
	    Integer val4=con_4.convert("java");
	   
		Converter con_3=from->"sharejava".indexOf(from);
		Integer val3=con_3.convert("java");
		
		System.out.println(val4);
		System.out.println(val3);
	//---------------------------------------3.引用某類物件的例項方法-----------------------------------------
		MakeString ms1=(a,b,c)->a.substring(b,c);
		String str1=ms1.make("sharejava",5,9);
		
		MakeString ms2=String::substring;
		String str2=ms2.make("sharejava",5,9);
		
		System.out.println(str1);
		System.out.println(str2);
	//---------------------------------------4.引用構造方法-----------------------------------------
		GetJFrame g1=title->new javax.swing.JFrame(title);
		javax.swing.JFrame jf1=g1.get("我的視窗");
		
		GetJFrame g2=javax.swing.JFrame::new;
		javax.swing.JFrame jf2=g2.get("我的視窗");
		
		jf1.setVisible(true);
		jf1.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE );
		jf1.setBounds(200,200,500,400);
	}
}

3.使用Lambda表示式呼叫Arrays類的方法

我們發現Arrays類的API下包含大量的方法需要函式式介面引數:

static void parallelPrefix(double[] array, DoubleBinaryOperator op)
    Cumulates, in parallel, each element of the given array in place, using the supplied function.
static void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)
    Performs parallelPrefix(double[], DoubleBinaryOperator) for the given subrange of the array.
static void parallelPrefix(int[] array, IntBinaryOperator op)
    Cumulates, in parallel, each element of the given array in place, using the supplied function.
static void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)
    Performs parallelPrefix(int[], IntBinaryOperator) for the given subrange of the array.
static void parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)
    Performs parallelPrefix(long[], LongBinaryOperator) for the given subrange of the array.
static void parallelPrefix(long[] array, LongBinaryOperator op)
    Cumulates, in parallel, each element of the given array in place, using the supplied function.
static <T> void parallelPrefix(T[] array, BinaryOperator<T> op)
    Cumulates, in parallel, each element of the given array in place, using the supplied function.
static <T> void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)
    Performs parallelPrefix(Object[], BinaryOperator) for the given subrange of the array.
static void parallelSetAll(double[] array, IntToDoubleFunction generator)
    Set all elements of the specified array, in parallel, using the provided generator function to compute each element.
static void parallelSetAll(int[] array, IntUnaryOperator generator)
    Set all elements of the specified array, in parallel, using the provided generator function to compute each element.
static void parallelSetAll(long[] array, IntToLongFunction generator)
    Set all elements of the specified array, in parallel, using the provided generator function to compute each element.
static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator)
    Set all elements of the specified array, in parallel, using the provided generator function to compute each element.
static <T> void parallelSort(T[] a, Comparator<? super T> cmp)
    Sorts the specified array of objects according to the order induced by the specified comparator.
static <T extends Comparable<? super T>> void parallelSort(T[] a, int fromIndex, int toIndex)
    Sorts the specified range of the specified array of objects into ascending order, according to the natural         ordering of its elements.
static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)
    Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
static void setAll(double[] array, IntToDoubleFunction generator)
    Set all elements of the specified array, using the provided generator function to compute each element.
static void setAll(int[] array, IntUnaryOperator generator)
    Set all elements of the specified array, using the provided generator function to compute each element.
static void setAll(long[] array, IntToLongFunction generator)
    Set all elements of the specified array, using the provided generator function to compute each element.
static <T> void setAll(T[] array, IntFunction<? extends T> generator)
    Set all elements of the specified array, using the provided generator function to compute each element.
這裡演示了其中部分方法的應用:

import java.util.Arrays;
public class LambdaArrays
{
	public static void main(String[] args)
	{
		String[] arr1=new String[]{"html","c#","c","c++","java"};
		//目標型別是Comparator根據字串的長度判斷字串的大小,然後排序
		Arrays.parallelSort(arr1,(o1,o2)->o1.length()-o2.length());
		System.out.println(Arrays.toString(arr1));
		
		int[] arr2=new int[]{-9,1,5,-4,12};
		//目標型別是 IntBinaryOperator,根據前後兩個元素計算當前元素的值
		Arrays.parallelPrefix(arr2,(left,right)->left*right);
		System.out.println(Arrays.toString(arr2));
		
	}
}