不來看看 Lambda 爽一下?

2020-10-21 Android編程精選

Python實戰社群

Java實戰社群

長按識別下方二維碼,按需求添加

掃碼關注添加客服

進Python社群▲

掃碼關注添加客服

進Java社群


作者丨9龍

juejin.im/post/6844903849753329678

引言

java8最大的特性就是引入Lambda表達式,即函數式編程,可以將行為進行傳遞。「總結就是:使用不可變值與函數,函數對不可變值進行處理,映射成另一個值。」

一、java重要的函數式接口

1、什麼是函數式接口

「函數接口是只有一個抽象方法的接口,用作 Lambda 表達式的類型。使用@FunctionalInterface註解修飾的類,編譯器會檢測該類是否只有一個抽象方法或接口,否則,會報錯。可以有多個默認方法,靜態方法。」

1.1 java8自帶的常用函數式接口。
函數接口抽象方法功能參數返回類型示例
Predicatetest(T t)判斷真假Tboolean9龍的身高大於185cm嗎?
Consumeraccept(T t)消費消息Tvoid輸出一個值
FunctionR apply(T t)將T映射為R(轉換功能)TR獲得student對象的名字
SupplierT get()生產消息NoneT工廠方法
UnaryOperatorT apply(T t)一元操作TT邏輯非(!)
BinaryOperatorapply(T t, U u)二元操作(T,T)(T)求兩個數的乘積(*)
public class Test {
    public static void main(String[] args) {
        Predicate<Integer> predicate = x -> x > 185;
        Student student = new Student("9龍", 23, 175);
        System.out.println(
            "9龍的身高高於185嗎?:" + predicate.test(student.getStature()));

        Consumer<String> consumer = System.out::println;
        consumer.accept("命運由我不由天");

        Function<Student, String> function = Student::getName;
        String name = function.apply(student);
        System.out.println(name);

        Supplier<Integer> supplier = 
            () -> Integer.valueOf(BigDecimal.TEN.toString());
        System.out.println(supplier.get());

        UnaryOperator<Boolean> unaryOperator = uglily -> !uglily;
        Boolean apply2 = unaryOperator.apply(true);
        System.out.println(apply2);

        BinaryOperator<Integer> operator = (x, y) -> x * y;
        Integer integer = operator.apply(2, 3);
        System.out.println(integer);

        test(() -> "我是一個演示的函數式接口");
    }

    /**
     * 演示自定義函數式接口使用
     *
     * @param worker
     */
    public static void test(Worker worker) {
        String work = worker.work();
        System.out.println(work);
    }

    public interface Worker {
        String work();
    }
}
//9龍的身高高於185嗎?:false
//命運由我不由天
//9龍
//10
//false
//6
//我是一個演示的函數式接口

以上演示了lambda接口的使用及自定義一個函數式接口並使用。下面,我們看看java8將函數式接口封裝到流中如何高效的幫助我們處理集合。

「注意:Student::getName」例子中這種編寫lambda表達式的方式稱為「方法引用。「格式為」ClassNmae::methodName」。是不是很神奇,java8就是這麼迷人。

「示例:本篇所有示例都基於以下三個類。OutstandingClass:班級;Student:學生;SpecialityEnum:特長。」

1.2 惰性求值與及早求值

「惰性求值:只描述Stream,操作的結果也是Stream,這樣的操作稱為惰性求值。」

惰性求值可以像建造者模式一樣鏈式使用,最後再使用及早求值得到最終結果。

「及早求值:得到最終的結果而不是Stream,這樣的操作稱為及早求值。」

2、常用的流

2.1 collect(Collectors.toList())

「將流轉換為list。還有toSet(),toMap()等。及早求值」

public class TestCase {
    public static void main(String[] args) {
        List<Student> studentList = Stream.of(new Student("路飛", 22, 175),
                new Student("紅髮", 40, 180),
                new Student("白鬍子", 50, 185)).collect(Collectors.toList());
        System.out.println(studentList);
    }
}
//輸出結果
//[Student{name='路飛', age=22, stature=175, specialities=null}, 
//Student{name='紅髮', age=40, stature=180, specialities=null}, 
//Student{name='白鬍子', age=50, stature=185, specialities=null}]

2.2 filter

顧名思義,起「過濾篩選」的作用。「內部就是Predicate接口。惰性求值。」

比如我們篩選出出身高小於180的同學。

public class TestCase {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>(3);
        students.add(new Student("路飛", 22, 175));
        students.add(new Student("紅髮", 40, 180));
        students.add(new Student("白鬍子", 50, 185));

        List<Student> list = students.stream()
            .filter(stu -> stu.getStature() < 180)
            .collect(Collectors.toList());
        System.out.println(list);
    }
}
//輸出結果
//[Student{name='路飛', age=22, stature=175, specialities=null}]

2.3 map

「轉換功能,內部就是Function接口。惰性求值」

public class TestCase {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>(3);
        students.add(new Student("路飛", 22, 175));
        students.add(new Student("紅髮", 40, 180));
        students.add(new Student("白鬍子", 50, 185));

        List<String> names = students.stream().map(student -> student.getName())
                .collect(Collectors.toList());
        System.out.println(names);
    }
}
//輸出結果
//[路飛, 紅髮, 白鬍子]

例子中將student對象轉換為String對象,獲取student的名字。

2.4 flatMap

「將多個Stream合併為一個Stream。惰性求值」

public class TestCase {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>(3);
        students.add(new Student("路飛", 22, 175));
        students.add(new Student("紅髮", 40, 180));
        students.add(new Student("白鬍子", 50, 185));

        List<Student> studentList = Stream.of(students,
                asList(new Student("艾斯", 25, 183),
                        new Student("雷利", 48, 176)))
                .flatMap(students1 -> students1.stream()).collect(Collectors.toList());
        System.out.println(studentList);
    }
}
//輸出結果
//[Student{name='路飛', age=22, stature=175, specialities=null}, 
//Student{name='紅髮', age=40, stature=180, specialities=null}, 
//Student{name='白鬍子', age=50, stature=185, specialities=null}, 
//Student{name='艾斯', age=25, stature=183, specialities=null},
//Student{name='雷利', age=48, stature=176, specialities=null}]

調用Stream.of的靜態方法將兩個list轉換為Stream,再通過flatMap將兩個流合併為一個。

2.5 max和min

我們經常會在集合中「求最大或最小值」,使用流就很方便。「及早求值。」

public class TestCase {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>(3);
        students.add(new Student("路飛", 22, 175));
        students.add(new Student("紅髮", 40, 180));
        students.add(new Student("白鬍子", 50, 185));

        Optional<Student> max = students.stream()
            .max(Comparator.comparing(stu -> stu.getAge()));
        Optional<Student> min = students.stream()
            .min(Comparator.comparing(stu -> stu.getAge()));
        //判斷是否有值
        if (max.isPresent()) {
            System.out.println(max.get());
        }
        if (min.isPresent()) {
            System.out.println(min.get());
        }
    }
}
//輸出結果
//Student{name='白鬍子', age=50, stature=185, specialities=null}
//Student{name='路飛', age=22, stature=175, specialities=null}

「max、min接收一個Comparator」(例子中使用java8自帶的靜態函數,只需要傳進需要比較值即可。)並且返回一個Optional對象,該對象是java8新增的類,專門為了防止null引發的空指針異常。可以使用max.isPresent()判斷是否有值;可以使用max.orElse(new Student()),當值為null時就使用給定值;也可以使用max.orElseGet(() -> new Student());這需要傳入一個Supplier的lambda表達式。

2.6 count

「統計功能,一般都是結合filter使用,因為先篩選出我們需要的再統計即可。及早求值」

public class TestCase {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>(3);
        students.add(new Student("路飛", 22, 175));
        students.add(new Student("紅髮", 40, 180));
        students.add(new Student("白鬍子", 50, 185));

        long count = students.stream().filter(s1 -> s1.getAge() < 45).count();
        System.out.println("年齡小於45歲的人數是:" + count);
    }
}
//輸出結果
//年齡小於45歲的人數是:2

2.7 reduce

「reduce 操作可以實現從一組值中生成一個值」。在上述例子中用到的 count 、 min 和 max 方法,因為常用而被納入標準庫中。事實上,這些方法都是 reduce 操作。「及早求值。」

public class TestCase {
    public static void main(String[] args) {
        Integer reduce = Stream.of(1, 2, 3, 4).reduce(0, (acc, x) -> acc+ x);
        System.out.println(reduce);
    }
}
//輸出結果
//10

我們看得reduce接收了一個初始值為0的累加器,依次取出值與累加器相加,最後累加器的值就是最終的結果。

二、高級集合類及收集器

1 轉換成值

「收集器,一種通用的、從流生成複雜值的結構。」只要將它傳給 collect 方法,所有的流就都可以使用它了。標準類庫已經提供了一些有用的收集器,

「以下示例代碼中的收集器都是從 java.util.stream.Collectors 類中靜態導入的。」

public class CollectorsTest {
    public static void main(String[] args) {
        List<Student> students1 = new ArrayList<>(3);
        students1.add(new Student("路飛", 23, 175));
        students1.add(new Student("紅髮", 40, 180));
        students1.add(new Student("白鬍子", 50, 185));

        OutstandingClass ostClass1 = new OutstandingClass("一班", students1);
        //複製students1,並移除一個學生
        List<Student> students2 = new ArrayList<>(students1);
        students2.remove(1);
        OutstandingClass ostClass2 = new OutstandingClass("二班", students2);
        //將ostClass1、ostClass2轉換為Stream
        Stream<OutstandingClass> classStream = Stream.of(ostClass1, ostClass2);
        OutstandingClass outstandingClass = biggestGroup(classStream);
        System.out.println("人數最多的班級是:" + outstandingClass.getName());

        System.out.println("一班平均年齡是:" + averageNumberOfStudent(students1));
    }

    /**
     * 獲取人數最多的班級
     */
    private static OutstandingClass biggestGroup(Stream<OutstandingClass> outstandingClasses) {
        return outstandingClasses.collect(
                maxBy(comparing(ostClass -> ostClass.getStudents().size())))
                .orElseGet(OutstandingClass::new);
    }

    /**
     * 計算平均年齡
     */
    private static double averageNumberOfStudent(List<Student> students) {
        return students.stream().collect(averagingInt(Student::getAge));
    }
}
//輸出結果
//人數最多的班級是:一班
//一班平均年齡是:37.666666666666664

maxBy或者minBy就是求最大值與最小值。

2 轉換成塊

「常用的流操作是將其分解成兩個集合,Collectors.partitioningBy幫我們實現了,接收一個Predicate函數式接口。」

將示例學生分為會唱歌與不會唱歌的兩個集合。

public class PartitioningByTest {
    public static void main(String[] args) {
        //省略List<student> students的初始化
        Map<Boolean, List<Student>> listMap = students.stream().collect(
            Collectors.partitioningBy(student -> student.getSpecialities().
                                      contains(SpecialityEnum.SING)));
    }
}

3 數據分組

數據分組是一種更自然的分割數據操作,與將數據分成 ture 和 false 兩部分不同,「可以使」「用任意值對數據分組。Collectors.groupingBy接收一個Function做轉換。」

「我們使用groupingBy將根據進行分組為圓形一組,三角形一組,正方形一組。」

例子:根據學生第一個特長進行分組

public class GroupingByTest {
    public static void main(String[] args) {
        //省略List<student> students的初始化
         Map<SpecialityEnum, List<Student>> listMap = 
             students.stream().collect(
             Collectors.groupingBy(student -> student.getSpecialities().get(0)));
    }
}

「Collectors.groupingBy與SQL 中的 group by 操作是一樣的。」

4 字符串拼接

如果將所有學生的名字拼接起來,怎麼做呢?通常只能創建一個StringBuilder,循環拼接。使用Stream,使用Collectors.joining()簡單容易。

public class JoiningTest {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>(3);
        students.add(new Student("路飛", 22, 175));
        students.add(new Student("紅髮", 40, 180));
        students.add(new Student("白鬍子", 50, 185));

         String names = students.stream()
             .map(Student::getName).collect(Collectors.joining(",","[","]"));
        System.out.println(names);
    }
}
//輸出結果
//[路飛,紅髮,白鬍子]

「joining接收三個參數,第一個是分界符,第二個是前綴符,第三個是結束符。也可以不傳入參數Collectors.joining(),這樣就是直接拼接。」

總結

本篇主要從實際使用講述了常用的方法及流,使用java8可以很清晰表達你要做什麼,代碼也很簡潔。本篇例子主要是為了講解較為簡單,大家可以去使用java8重構自己現有的代碼,自行領會lambda的奧妙。本文說的Stream要組合使用才會發揮更大的功能,鏈式調用很迷人,根據自己的業務去做吧。

-- END --

程式設計師專欄 掃碼關注填加客服 長按識別下方二維碼進群

近期精彩內容推薦:  

 

 

 

 





在看點這裡好文分享給更多人↓↓

相關焦點

  • Python中的Lambda表達式
    它們只能包含一個表達式,因此不適合具有控制流語句的函數。Lambda函數的語法lambda arguments: expressionLambda函數可以具有任意數量的參數,但只能有一個表達式。>def square1(num):return num ** 2print(square(5)) # 輸出: 25在上面的lambda示例中,lambda x: x ** 2產生一個可以與任何名稱關聯的匿名函數對象。
  • Lambda 表達式入門,這篇夠了!
    這個註解往往會和 lambda 表達式一起出現。Lambda 基礎語法我們這裡給出六個接口,後文的全部操作都利用這六個接口來進行闡述。import lambda.interfaces.*;    public class Test2 {      public static void main(String[] args) {            //1.簡化參數類型,可以不寫參數類型,但是必須所有參數都不寫
  • Lambda 表達式入門,看這篇就夠了!
    這個註解往往會和 lambda 表達式一起出現。Lambda 基礎語法我們這裡給出六個接口,後文的全部操作都利用這六個接口來進行闡述。*;public class Test2 {    public static void main(String[] args) {        //1.簡化參數類型,可以不寫參數類型,但是必須所有參數都不寫
  • Python零基礎入門教程,如何使用lambda、filter和map函數?
    大綱函數類型定義及特性lambda函數定義及使用filter函數定義及使用map函數定義及使用引入函數類型概念函數類型定義:python中任意一個函數都有數據類型,這種數據類型是function(函數類型)
  • Lambda 插件錢包全球懸賞計劃來襲,百萬獎池重磅開啟
    錢包下載地址:https://www.lambdastorage.com/walletpages目的就是要保證Lambda錢包的安全性,確保所有用戶是用錢包暢遊Lambda生態網絡。 對於為我們提出有價值的錢包安全漏洞的用戶,我們將會為大家送出萬元USDT獎勵!快來了解一下活動內容吧!
  • 使用NodeJS Lambda函數查詢RDS MySQL資料庫
    最近,我第一次嘗試了AWS lambda。我的任務是查詢RDS MySQL資料庫。在這篇文章中,我將分享我的經驗。我得到的錯誤如下:我發現AWS本身不支持我們使用的MySQL模塊,因此我們需要提供捆綁的ZIP文件作為lambda函數。
  • lambda qt 參數 槽函數 - CSDN
    高級線程中使用QtConcurrent命名空間中的run接口支持匿名函數,用起來簡直爽得不要不要的。關於匿名函數(Lambda表達式)可點擊:這是一個函數[](){}關於QVariant與匿名函數的使用可點擊:Qt開源網絡庫[5]-lambda支持
  • Java中Lambda表達式的5種不同語法
    Arrays.sort(arr, (String m, String n) -> Integer.compare(m.length(), n.length()));System.out.println(Arrays.toString(arr));lambda
  • Lambda 表達式,簡潔優雅就是生產力!
    直接這樣說可能還是有點讓人困擾,我們繼續看看例子。我們給上面的aBlockOfCode加上一個類型:這種只有一個接口函數需要被實現的接口類型,我們叫它」函數式接口「。我們可以對比一下Lambda表達式和傳統的Java對同一個接口的實現:這兩種寫法本質上是等價的。但是顯然,Java 8中的寫法更加優雅簡潔。
  • 編譯器說 Lambda 表達式中的變量必須是 final 的,我偏不信 | 原力計劃
    作者 | 沉默王二來源 | CSDN博客專家出品 | CSDN(ID:CSDNnews)偶爾,我們需要在 Lambda 表達式中修改變量的值,但如果直接嘗試修改的話,編譯器不會視而不見聽而不聞,它會警告我們說:「variable used in lambda
  • 黃明昊要「挨打」了,皮一下很爽,一直皮一直爽,會交不到女朋友
    黃明昊要「挨打」了,皮一下很爽,一直皮一直爽,會交不到女朋友!知道黃明昊一向都很皮,大部分時間模仿自己的師父楊迪逗逗大家開心。逗比的本質掩蓋不了,然而暖男身份竟然也要被自己玩壞了。新綜藝上,直男式的聊天方式讓同場嘉賓接不住梗。
  • 用Python統計哪個星座的富豪最多,看看你有沒有成為富豪的潛質
    前言 本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。 最近經常看到關於星座的推送,我其實不信這個,但是由於推送得太多了,總有幾篇會讓我好奇地點進去看看。然後想想其實用Python寫一篇星座相關的文章也不錯。 正好,Python計算一個人的星座特別簡單,因為每個星座的日期是固定的。我們只需要四行代碼就能計算:
  • 那來揭秘一下這款清潔面膜到底值不值得入?
    上期咱們分享了補水美白面膜,今天來個清潔面膜康康吧!2、 塗面膜前一定做好保溼,先塗水乳或者精華,再上面膜,這樣既有效的保證你的皮膚溼潤,不會極度缺水,還能在撕拉麵膜的時候不那麼酸爽。(小阿姨偷偷的告訴你呦!水乳和精華,精華效果最好)3、 在確保水乳或者精華完全吸收就可以塗面膜了。面膜不用塗太厚,但是要快,因為它幹的也比較快。
  • 阿里天池機器學習競賽項目總結,特徵工程了解一下!
    來源:阿里雲天池,案例:機器學習實踐  業界廣泛流傳著這樣一句話:「數據和特徵決定了機器學習的上限,而模型和算法只是逼近這個上限而已」,由此可見特徵工程在機器學習中的重要性,今天我們將通過《阿里雲天池大賽賽題解析——機器學習篇》中的【天貓 用戶重複購買預測】案例來深入解析特徵工程在實際商業場景中的應用。
  • 木耳一下鍋就「炸鍋」,太嚇人?掌握4技巧,保證不炸鍋,更脆爽
    這樣您就可以繼續免費收到文章了,每天都有分享,完全是「免費訂閱」,請放心關注                                                                                              黑木耳是一種常見的食材,它的營養豐富,口感脆爽嫩滑
  • 媽扭頭見「兇手」一臉爽翻天:啊~好爽!
    結果剛一扭頭就發現Bowie一臉爽翻天的表情癱在紙箱上面,瞬間讓媽掌握情況!Bowie:啊~就是爽~貓薄荷別稱(貓大麻)能夠幫助貓咪排出毛球或者緩解壓力的功效,通常貓咪攝取後會出現翻滾、興奮、流口水的表現,屬於正常的行為,可是Bowie這一下也吸太多了!
  • 冬季的大白菜,怎麼炒出來酸辣又脆爽,還不出水?小技巧教給你
    酸辣大白菜,怎麼炒出來酸辣又脆爽,還不出水?