接上一篇睡互,lambda表達式的基本邏輯,這次總結(jié)了接種特殊的使用方法陵像。
一、對象::實列方法名
特點:
1寇壳、表達式體調(diào)用的方法醒颖,在接口中已經(jīng)實現(xiàn),不需要再在調(diào)用的時候?qū)崿F(xiàn)。使用此方法;
2顽聂、調(diào)用的方法蹋绽,參數(shù)列表和返回值殃恒,既已經(jīng)實現(xiàn)的方法簽名晒旅,和Lambda表達式一致馏臭;
@Test
public void test1(){
Consumer<String> consumer1 = (x)-> System.out.println(x);
//上面表達式的簡寫如下
Consumer<String> consumer2 = System.out::println;
consumer1.accept("This is output sequence");//控制它輸出:This is output sequence
}
Consumer是Java自帶的接口彻况,是“消費性”接口挺庞,以上代碼的大白話可以翻譯成:接口的accept方法接受一個參數(shù)晰赞,它的據(jù)體實現(xiàn)是打印接收到的那句話。此時可以將參數(shù)省略选侨。該接口部分代碼如下:
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
}
二掖鱼、類::靜態(tài)方法名
@Test
public void test3(){
Comparator<Integer> com = (x,y)->Integer.compare(x,y);
System.out.println(com.compare(20,22));//輸出:-1
Comparator<Integer> compare = Integer::compare;
System.out.println(compare.compare(20,22));//輸出:-1
}
Comparator接口的“比較”部分代碼如下:
@FunctionalInterface
public interface Comparator<T> {
/**
* Compares its two arguments for order. Returns a negative integer,
* zero, or a positive integer as the first argument is less than, equal
* to, or greater than the second.
* @param o1 the first object to be compared.
* @param o2 the second object to be compared.
* @return a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the
* second.
* @throws NullPointerException if an argument is null and this
* comparator does not permit null arguments
* @throws ClassCastException if the arguments' types prevent them from
* being compared by this comparator.
*/
int compare(T o1, T o2);
}
Integer的compare方法定義如下:
public final class Integer extends Number implements Comparable<Integer> {
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
}
三、類::實列方法
@Test
public void test4(){
BiPredicate<String,String> bp = (x ,y )->x.equals(y);
//此方法使用限制:
//x是方法的調(diào)用者援制,y是方法參數(shù)
BiPredicate<String, String> bp2= String::equals;
System.out.println(bp2.test("a","ab")); //output: false
}
BiPredicate接口部分代碼如下:
@FunctionalInterface
public interface BiPredicate<T, U> {
/**
* Evaluates this predicate on the given arguments.
*
* @param t the first input argument
* @param u the second input argument
* @return {@code true} if the input arguments match the predicate,
* otherwise {@code false}
*/
boolean test(T t, U u);
}
String.equals()的簽名如下:
public boolean equals(Object anObject) {}
四戏挡、構(gòu)造器引用
格式: classname::new
被調(diào)用的構(gòu)造器的參數(shù)列表,與接口中抽象方法的參數(shù)列表一致晨仑。
@Test
public void test5(){
//調(diào)用哪種構(gòu)造參數(shù)褐墅,取決于接口,
//==>> 如Supplier的方法洪己,是無參函數(shù)
//==>> Function的方法妥凳,接受一個參數(shù),返回一個類型
//調(diào)用無參構(gòu)造
Supplier<Employee> emp = ()->new Employee(); //實例化返回一個employee對象
Supplier<Employee> emp1= Employee::new;
//調(diào)用码泛,根據(jù)無參構(gòu)造猾封,返回一個對象
Employee employee = emp.get();
//調(diào)用一個參數(shù)的構(gòu)造函數(shù)
Function<Integer, Employee> fun1 = (number)->new Employee(number);
Function<Integer, Employee> fun2 = Employee::new ;
//調(diào)用,根據(jù)用戶編號返回用戶
Employee employeer = fun1.apply(201);
}
Java自帶Supplier接口噪珊,代碼如下:
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Function接口的部分代碼如下:
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
五晌缘、數(shù)組引用
@Test
public void test6(){
Function<Integer, String[]> fun = (x)->new String[x];
String[] strings = fun.apply(10);
System.out.println(strings.length);//輸出10
Function<Integer, String[]> fun2= String[]::new;
String[] strings1 = fun2.apply(20);
System.out.println(strings1.length);//輸出20
}
數(shù)組也是一個對象。
以上五種寫法痢站,歸根結(jié)底磷箕,還是不變的先定義接口,然后定義一個方法阵难,將接口作為形參岳枷,最后使用lambda表達式作為實參傳入方法,實現(xiàn)接口中的方法的具體實現(xiàn)呜叫。