JDK中有許多函數式接口,也會有許多方法會使用函數式接口作為參數,同時在各種源碼中也大量使用了這些方法,那么我們在實際工作中應該如何使用!我們就來盤一盤,這樣也有助于寫出優雅的代碼,使我們在閱讀源碼時事半功倍。
1 JDK中常見的Lamada表達式
Java中可以使用Lamada表達式的接口都有@FunctionalInterface注解。
先來看看util.function包下面含有FunctionalInterface注解的接口。一屏顯示不全,可見功能非常齊全。
鑒于常用的一些函數式接口有Function/Consumer/Supplier/Predicate以及Runnable等。本篇介紹這幾類接口。
1.1 Runnable
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
Runnable 使用Lamada方式書寫時,無參數,無返回值。最終執行的是run方法
使用Demo
new Thread(() - > {
System.out.println("JavaNorth Runnable");
}).start();
1.2 Function
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);
default < V > Function< V, R > compose(Function< ? super V, ? extends T > before) {
Objects.requireNonNull(before);
return (V v) - > apply(before.apply(v));
}
default < V > Function< T, V > andThen(Function< ? super R, ? extends V > after) {
Objects.requireNonNull(after);
return (T t) - > after.apply(apply(t));
}
static < T > Function< T, T > identity() {
return t - > t;
}
}
Function接受一個參數T,并且有返回值 R, 其實現也主要是實現此方法 R apply(T t);
Function 的一個示例:
List< String > list = new ArrayList< String >();
List< String > collect = list.stream().map((x) - > {
return "Java North Function" + x;
}).collect(Collectors.toList());
上述示例中是一個stream的map方法。其中x為輸入參數,『"Java North and" + x』為輸出。
1.3 Consumer
Consumer表示接受一個參數,沒有返回值的操作,主要方法為 void accept(T t);
@FunctionalInterface
public interface Consumer< T > {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
default Consumer< T > andThen(Consumer< ? super T > after) {
Objects.requireNonNull(after);
return (T t) - > { accept(t); after.accept(t); };
}
}
1.3.1 Consumer 在Java中的應用
常見的有List的forEach等。
list.forEach(x - > System.out.println( "Java North Consumer test " + x));
x為參數,輸出語句直接執行。
下面的map的forEach參數為BiConsumer,其入參有兩個。
Map String,String > map = new HashMap >();
map.forEach((K,V) - > {
System.out.println("Java North Big Consumer MAP key : " +K + " value: "+V );
});
1.3.2 自定義帶有Consumer的方法
public class ConsumerBiConsumerDemo {
public static void main(String[] args) {
Consumer< String > conString = (x) - > System.out.println(x.toUpperCase());
conString.accept("i love java north ");
BiConsumer< String, String > biCon = (x,y) - > System.out.println (x + y);
biCon.accept("i love ","java");
List< Person > plist = Arrays.asList(new Person("Java"),new Person("North"));
acceptAllEmployee(plist,p - > System.out.println(p.name));
acceptAllEmployee(plist,person - > {person.name = "unknow";});
acceptAllEmployee(plist,person - > System.out.println(person.name));
}
public static void acceptAllEmployee(List< Person > plist, Consumer< Person > con){
plist.forEach(person - > {con.accept(person);});
}
}
class Person{
public String name;
public Person (String name){
this.name = name;
}
}
1.4 Supplier
Supplier沒有參數,有一個返回值。
@FunctionalInterface
public interface Supplier< T > {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Supplier 示例如下:
public class SupplierDemo {
public static void main(String[] args) {
SupplierDemo sdemo = new SupplierDemo();
Supplier< LocalDateTime > s = () - > LocalDateTime.now();
LocalDateTime localDateTime = s.get();
System.out.println(s.get());
Supplier< List > listSupplier = sdemo.listSupplier();
List list = listSupplier.get();
Person person = personFactory(Person::new);
System.out.println(person);
Person javaNorth = personFactory(() - > new Person("JavaNorth"));
System.out.println(javaNorth);
}
public Supplier< List > listSupplier(){
return ArrayList::new;
}
public static Person personFactory(Supplier< ? extends Person > s){
Person person = s.get();
if(person.getName() == null) person.setName("default");
person.setAge(18);
return person;
}
static class Person {
String name;
int age;
public Person() { }
public Person(String name) {
this.name = name;
}
。。。
}
}
1.5 Predicate
主要方法為test,其主要是傳入一個參數,返回一個boolean類型的值。
@FunctionalInterface
public interface Predicate< T > {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
default Predicate< T > and(Predicate< ? super T > other) {
Objects.requireNonNull(other);
return (t) - > test(t) && other.test(t);
}
default Predicate< T > negate() {
return (t) - > !test(t);
}
default Predicate< T > or(Predicate< ? super T > other) {
Objects.requireNonNull(other);
return (t) - > test(t) || other.test(t);
}
......
}
Predicate簡單示例:
public class PredicateDemo {
public static void main(String[] args) {
Predicate< Integer > predicate = (s) - > s > 5;
Predicate< Integer > predicate2 = (s) - > s > 8;
System.out.println(" 3 大于5 ? " + predicate.test(3));
System.out.println(" 6 大于5 ? " + predicate.test(6));
System.out.println("7 大于5 and 大于8 " + predicate.and(predicate2).test(7));
System.out.println("7 大于5 or 大于8 " + predicate.or(predicate2).test(7));
List< Integer > list = Arrays.asList(3,5,6,2,8,4,7,9);
List< Integer > collect = list.stream().filter(predicate).collect(Collectors.toList());
System.out.println(list);
System.out.println(collect);
}
}
上述代碼運行結果
3 大于5 ? false
6 大于5 ? true
7 大于5 and 大于8 false
7 大于5 or 大于8 true
[3, 5, 6, 2, 8, 4, 7, 9]
[6, 8, 7, 9]
2 常用的Lamada參數特征
Lamada 的一些表達式將方法的一些執行邏輯放到了參數中,使得方法的返回值根據傳入的參數的邏輯而變化。從而實現了在一定的方法不變的情況下,使代碼執行傳入參數相關的邏輯。
常用的一些Lamada使用如下:
Runnable 無入參,無返回值 。
() - > { System.out.println(strF.apply("javaNorth Runnable"));}
Function 有入參,有返回值
Function strF = (s) - > { return s + "javaNorth Function"; };
System.out.println(strF.apply("TEST "));
Consumer有入參,無返回值。
Consumer< String > srtC = (s) - > {System.out.println(s + "javaNorth TEST ");};
srtC.accept("Hello World");
Supplier 無入參,有返回值。
Supplier< String > srtP = () - > {return "I love avaNorth ";};
System.out.println(srtP.get());
Predicate 有入參,返回一個boolean類型的值
Predicate< Integer > predicate = (s) - > s > 5;
System.out.println(" 3 大于5 ? " + predicate.test(3));
3 自定義Lamada函數式接口
結合之前的常用Lamada函數的介紹,下面我們自定義一個簡單的函數式接口。
@FunctionalInterface
public interface MyFunction< T > {
public void print(T t);
}
public class MyFunctionDemo {
public static void printString(String str , MyFunction< String > myF){
myF.print(str);
}
public static void main(String[] args) {
printString("I love " , (str) - > {System.out.println(str + "Java North");});
}
}
其實很簡單,就是展示了一下自定義函數式接口的應用實例而已。
總結
函數式接口的使用可以簡化我們的代碼,很大程度上提高代碼的可讀性。在程序執行上可能性能稍微有所降低,但對開發帶來的便捷與好處是大于其性能上的微弱損失的。除了介紹的幾種常見的函數式編程以外,還有許多其他的函數式接口等著我們去利用。聽完這些話之后,軟軟猿妹也對常用的函數式接口有所了解,接下來下定決心要好好學一學函數式接口了。
-
接口
+關注
關注
33文章
8775瀏覽量
152397 -
函數
+關注
關注
3文章
4353瀏覽量
63296 -
JDK
+關注
關注
0文章
82瀏覽量
16682
發布評論請先 登錄
相關推薦
什么是正則表達式?正則表達式如何工作?哪些語法規則適用正則表達式?

如何創建正則的表達式?
防范表達式的失控
深入淺出boost正則表達式
Python正則表達式的學習指南

Python正則表達式指南

Oracle中常用的函數與表達式講解

Lambda表達式詳解
shell腳本基礎:正則表達式grep

評論