JAVA-進階(函數式編程)

JAVA-進階(函數式編程)

1.首先理解什麼是Lambda表達式?java

簡單來講,咱們定義一個函數,每每要去想怎麼調用,怎麼建立對象,可是Lambda表達式的思想是,我不用去管你怎麼去作這件事情,我只關心你在作什麼(有點繞口,多讀兩邊)。咱們只是爲了達到目的,過程和形式並不重要。編程

2.代碼體現?多線程

以Runnable接口爲例:app

 普通的寫法(實現內部類接口)ide

 1 public class Demo01Runnable {
 2     public static void main(String[] args) {
 3         // 匿名內部類
 4         Runnable task = new Runnable() {
 5             @Override
 6             public void run() { // 覆蓋重寫抽象方法
 7                 System.out.println("多線程任務執行!");
 8             }
 9         };
10         new Thread(task).start(); // 啓動線程
11     }
12 }

 Lambda表達式寫法函數式編程

public class Demo02LambdaRunnable {
    public static void main(String[] args) {
        new Thread(() -> System.out.println("多線程任務執行!")).start(); // 啓動線程
    }
}

3.Lambda優越性?函數

代碼簡潔,不用去寫一大堆對象內部類,另外一方面比較重要就是延時(會在下邊舉例)。ui

4.Lambda的寫法格式?spa

博主本身總結了一下,簡單就是一個接口,只有一個抽象方法。線程

()->System.out.println("多線程任務執行!")看做是一個向上轉型的對象就能夠了,()表明無參,固然是能夠加參數的,參數以逗號隔開,

System.out.println("多線程任務執行!")是你實現的抽象方法,在這裏也就是run方法。

至於返回值,要看你對抽象方法的定義,該返回什麼就返回什麼,和普通方法沒有區別。

省略格式建議不要用,少兩個括號感受用處不大。

5.函數式接口?

函數式接口,即適用於函數式編程場景的接口。而Java中的函數式編程體現就是Lambda。

首先介紹一個註解@FunctionalInterface,他是放在接口類上的,做用是檢查這個接口是否是隻有一個抽象方法,固然也能夠不寫。

1 @FunctionalInterface
2 public interface MyFunctionalInterface {
3 void myMethod();
4 }

下邊再說延時執行

public class Demo01Logger {
private static void log(int level, String msg) {
if (level == 1) {
System.out.println(msg);
}
}
public static void main(String[] args) {
String msgA = "Hello";
String msgB = "World";
String msgC = "Java";
log(1, msgA + msgB + msgC);
}
}
View Code

咱們會發現不管個人level等級是多少,字符串msgA + msgB + msgC都會拼接,可是咱們想一想這樣會不會形成一種資源的浪費當level=2時,我徹底不必去拼接字符串,由於字符串是不執行的。因此Lambda的延時執行就體現出來了

@FunctionalInterface
public interface MessageBuilder {
String buildMessage();
}
View Code
public class Demo02LoggerLambda {
private static void log(int level, MessageBuilder builder) {
if (level == 1) {
System.out.println(builder.buildMessage());
}
}
public static void main(String[] args) {
String msgA = "Hello";
String msgB = "World";
String msgC = "Java";
log(1, () ‐> msgA + msgB + msgC );
}
}
View Code

所謂的延時,就是說在我傳入接口對象參數時,我沒有執行方法體,只有何時調用buildMessage()這個方法的時候我纔會執行。這就解決了資源浪費問題。

6.經常使用的函數式接口?

他們都是已經定義好抽象方法的接口

Supplier接口:又稱爲供應商,不傳入參數,可是會返回

java.util.function.Supplier 接口僅包含一個無參的方法: T get() 。用來獲取一個泛型參數指定類型的對 象數據。因爲這是一個函數式接口,這也就意味着對應的Lambda表達式須要「對外提供」一個符合泛型類型的對象 數據。

import java.util.function.Supplier;
public class Demo08Supplier {
private static String getString(Supplier<String> function) {
return function.get();
}
public static void main(String[] args) {
String msgA = "Hello";
String msgB = "World";
System.out.println(getString(() ‐> msgA + msgB));
}
}
View Code

Consumer接口:又稱消費者,傳入參數,但無返回

java.util.function.Consumer 接口則正好與Supplier接口相反,它不是生產一個數據,而是消費一個數據, 其數據類型由泛型決定。

import java.util.function.Consumer;
public class Demo09Consumer {
private static void consumeString(Consumer<String> function) {
function.accept("Hello");
}
public static void main(String[] args) {
consumeString(s ‐> System.out.println(s));
}
}
View Code
import java.util.function.Consumer;
public class Demo10ConsumerAndThen {
private static void consumeString(Consumer<String> one, Consumer<String> two) {
one.andThen(two).accept("Hello");
}
public static void main(String[] args) {
consumeString(
s ‐> System.out.println(s.toUpperCase()),
s ‐> System.out.println(s.toLowerCase()));
}
}
View Code

Predicate接口:有時候咱們須要對某種類型的數據進行判斷,從而獲得一個boolean值結果,這時可使用 java.util.function.Predicate 接口。 

import java.util.function.Predicate;
public class Demo15PredicateTest {
private static void method(Predicate<String> predicate) {
boolean veryLong = predicate.test("HelloWorld");
System.out.println("字符串很長嗎:" + veryLong);
}
public static void main(String[] args) {
method(s ‐> s.length() > 5);
}
}
View Code
import java.util.function.Predicate;
public class Demo16PredicateAnd {
private static void method(Predicate<String> one, Predicate<String> two) {
boolean isValid = one.and(two).test("Helloworld");
System.out.println("字符串符合要求嗎:" + isValid);
}
public static void main(String[] args) {
method(s ‐> s.contains("H"), s ‐> s.contains("W"));
}
}
View Code

Function接口:接口用來根據一個類型的數據獲得另外一個類型的數據,前者稱爲前置條件, 後者稱爲後置條件。

import java.util.function.Function;
public class Demo11FunctionApply {
private static void method(Function<String, Integer> function) {
int num = function.apply("10");
System.out.println(num + 20);
}
public static void main(String[] args) {
method(s ‐> Integer.parseInt(s));
}
}
View Code
相關文章
相關標籤/搜索