Lambda表达式-Java8的新功能案例详解(二) 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));
		
	}
}

Lambda表达式-Java8的新功能案例详解(二) Lambda表达式的方法引用