方法引用
方法引用的使用
1.使用情景:当要传递给Lambda体的操作,已经有实现的方法,可以使用方法引用
2.方法引用,本质上就是lambda表达式,而Lambda表达式作为函数式接口的实例。所以方法引用,也是函数式接口的实例
3.使用格式:类(或对象) :: 方法名
4.具体分为如下的三种情况:
情况1 对象::非静态方法
情况2 类 :: 静态方法
情况3 类 :: 非静态方法
5. 方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表和返回值类型相同
代码练习
package com.sgl.lambda;
import com.sgl.reflect.Person;
import org.junit.Test;
import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* 方法引用的使用
*
* 1.使用情景:当要传递给Lambda体的操作,已经有实现的方法,可以使用方法引用
*
* 2.方法引用,本质上就是lambda表达式,而Lambda表达式作为函数式接口的实例。
* 所以方法引用,也是函数式接口的实例
*
* 3.使用格式:类(或对象) :: 方法名
*
* 4.具体分为如下的三种情况:
* 情况1 对象::非静态方法
* 情况2 类 :: 静态方法
* 情况3 类 :: 非静态方法
*
* 5. 方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型
* 与方法引用的方法的形参列表和返回值类型相同
*
* @author not_simple
* @version 1.0
* @date 2020/5/26 15:58
*/
public class MethodReferences {
/**
* 情况一:对象 :: 实例方法
* Consumer 中的void accept(T t)
* PrintStream 中的void println(T t)
*/
@Test
public void test1() {
//lambda表达式
Consumer<String> con1 = str -> System.out.println(str);
con1.accept("北京");
System.out.println("**************************************");
PrintStream printStream = System.out;
//方法应用
Consumer<String> con2 = printStream::println;
con2.accept("南京");
}
/**
* Supplier中 的T get()
* Person中的String getName()
*/
@Test
public void test2() {
Person person = new Person("sgl", 11);
//lambda表达式
Supplier<String> supplier = () -> person.getName();
System.out.println(supplier.get());
System.out.println("**************");
//方法引用
Supplier<String> supplier1 = person::getName;
System.out.println(supplier1.get());
}
/*
*情况二:类 :: 静态方法
* Comparator 中的int compare(T t1,T t2)
* Integer 中的 int compare (T t1,T t2)
*
*/
@Test
public void test3() {
//lambda表达式
Comparator<Integer> com1 = (t1, t2) -> Integer.compare(t1, t2);
System.out.println(com1.compare(11, 22));
System.out.println("****************************************");
//方法引用
Comparator<Integer> com2 = Integer::compare;
System.out.println(com2.compare(11, 2));
}
/**
* Function 中 R apply(T t)
* Math 中的Long round(Double d)
*/
@Test
public void test4() {
Function<Double,Long> fun = new Function<Double, Long>() {
@Override
public Long apply(Double aDouble) {
return Math.round(aDouble);
}
};
System.out.println(fun.apply(2.2));
System.out.println("*****************************************");
//lambda表达式
Function<Double, Long> function = d -> Math.round(d);
Long apply = function.apply(1.3);
System.out.println(apply);
System.out.println("******************************************");
//方法引用
Function<Double, Long> funNew = Math::round;
Long apply1 = funNew.apply(1.7);
System.out.println(apply1);
}
/*
* 情况三: 类:: 实例方法(有难度)
* Comparator中 int compare(T t1, T t2)
* Integer 中的int t1.compare(t2)
*/
@Test
public void test5(){
Comparator<Integer> com = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
};
System.out.println(com.compare(11,22));
System.out.println("***********************************");
//lambda表达式
Comparator<Integer> com1 = (o1,o2) -> o1.compareTo(o2);
System.out.println(com1.compare(11,2));
System.out.println("***********************************");
//方法引用
Comparator<Integer> com2 = Integer::compareTo;
System.out.println(com2.compare(11,12));
}
/*
* BiPredicate 中的boolean test(T t1,T t2);
* String 中的boolean t1.equeals(t2)
*/
@Test
public void test6() {
BiPredicate<String, String> biPredicate = new BiPredicate<String, String>() {
@Override
public boolean test(String s, String s2) {
return s.equals(s2);
}
};
System.out.println(biPredicate.test("abc", "aaa"));
System.out.println("**************************");
//lambda表达式
BiPredicate<String, String> bi1 = (s1,s2) -> s1.equals(s2);
System.out.println(bi1.test("abc","abcc"));
System.out.println("**************************");
//方法引用
BiPredicate<String,String> bi2 = String::equals;
System.out.println(bi2.test("abc","abc"));
}
/*
*Funcation 中R apply(T t)
*Employee 中String getName()
*/
@Test
public void test7(){
Person person = new Person("sgl" ,11);
Function<Person , String> fun =new Function<Person, String>() {
@Override
public String apply(Person person) {
return person.getName();
}
};
System.out.println(fun.apply(person));
System.out.println("***************************");
Function<Person , String> fun1 = person1 -> person1.getName();
System.out.println(fun1.apply(person));
System.out.println("***************************");
Function<Person , String> fun2 = Person::getName;
System.out.println(fun2.apply(person));
}
}
一. 构造器引用
和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。
抽象方法的返回值类型即为构造器所属的类的类型
二. 数组引用
大家可以把数组看做是一个特殊的类,则写法构造器引用就一致了
代码练习
package com.sgl.lambda;
import com.sgl.reflect.Person;
import org.junit.Test;
import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* 一. 构造器引用
* 和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。
* 抽象方法的返回值类型即为构造器所属的类的类型
*
*
* 二. 数组引用
* 大家可以把数组看做是一个特殊的类,则写法构造器引用就一致了
*
* @author not_simple
* @version 1.0
* @date 2020/5/27 14:47
*/
public class ConstructorReferences {
// 构造器引用
//Supplier中的 T get()
@Test
public void test1(){
Supplier<Person> sup1 = new Supplier<Person>() {
@Override
public Person get() {
return new Person();
}
};
System.out.println(sup1.get());
System.out.println("**********************************");
//lambda
Supplier<Person> sup2 = () -> new Person();
System.out.println(sup2.get());
System.out.println("**********************************");
//构造器引用
Supplier<Person> sup3 = Person::new;
System.out.println(sup3.get());
}
//Function 中的 R apply(T t)
@Test
public void test2(){
Function<Integer,Person> fun1 = age -> new Person(age);
System.out.println(fun1.apply(11));
System.out.println("*******************************************");
Function<Integer,Person> fun2 = Person::new;
System.out.println(fun2.apply(21));
}
//BiFunction 中的R apply(T t, U u)
@Test
public void test3(){
//lambda
BiFunction<String,Integer,Person> fun1 = (name,age) -> new Person(name,age);
System.out.println(fun1.apply("sgl",112));
System.out.println("***************************************");
//方法引用
BiFunction<String,Integer,Person> fun2 = Person::new;
System.out.println(fun1.apply("sglll",11));
}
/*
*数组引用
* Function中的R apply(T t)
*/
@Test
public void test(){
//lambda
Function<Integer,String[]> fun1 = length -> new String[length];
String[] arr1 = fun1.apply(5);
System.out.println(Arrays.toString(arr1));
//方法引用
Function<Integer,String[]> fun2 = String[]::new;
String[] arr2 = fun2.apply(4);
System.out.println(Arrays.toString(arr2));
}
}