一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

lambda表達式Stream流使用

 印度阿三17 2019-08-30

#Lambda表達式

Lambda允許把函數(shù)作為一個方法的參數(shù)(函數(shù)作為參數(shù)傳遞進方法中)


#lambda表達式本質(zhì)上就是一個匿名方法。比如下面的例子:

public int add(int x, int y) {
    return x   y;
}

轉(zhuǎn)成Lambda表達式后是這個樣子:

(int x, int y) -> x   y;

參數(shù)類型也可以省略,Java編譯器會根據(jù)上下文推斷出來:

(x, y) -> x   y; //返回兩數(shù)之和

或者

(x, y) -> { return x   y; } //顯式指明返回值

#java 8 in Action這本書里面的描述:

A lambda expression can be understood as a concise representation of an anonymous function
that can be passed around: it doesn’t have a name, but it has a list of parameters, a body, a
return type, and also possibly a list of exceptions that can be thrown. That’s one big definition;

#格式:

(x, y) -> x   y;
-----------------
(x,y):參數(shù)列表
x y  : body

#Lambda表達式用法實例:

package LambdaUse;

/**
 * @author zhangwenlong
 * @date 2019/8/25 15:17
 */
public class LambdaUse {

    public static void main(String[] args) {

        //匿名類實現(xiàn)
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello world");
            }
        };
        //Lambda方式實現(xiàn)
        Runnable r2 = ()-> System.out.println("Hello world");

        process(r1);
        process(r2);
        process(()-> System.out.println("Hello world"));
    }
    private  static void  process(Runnable r){
        r.run();
    }
}

輸出的結(jié)果:

Hello world
Hello world
Hello world

FunctionalInterface注解修飾的函數(shù)

#predicate的用法

package LambdaUse;

import java.applet.Applet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.LongPredicate;
import java.util.function.Predicate;

/**
 * @author zhangwenlong
 * @date 2019/8/25 15:30
 */
public class PredicateTest {

    //通過名字(Predicate實現(xiàn))
    private static List<Apple> filterApple(List<Apple> list, Predicate<Apple> predicate){
            List<Apple> appleList = new ArrayList<>();
            for(Apple apple : list){
                if(predicate.test(apple)){
                    appleList.add(apple);
                }
            }
            return appleList;
    }
    //通過重量(LongPredicate實現(xiàn))
    private static List<Apple> filterWeight(List<Apple> list, LongPredicate predicate){
        List<Apple> appleList = new ArrayList<>();
        for(Apple apple : list){
            if(predicate.test(apple.getWeight())){
                appleList.add(apple);
            }
        }
        return appleList;
    }

    public static void main(String[] args) {

        List<Apple> list = Arrays.asList(new Apple("蘋果",120),new Apple("香蕉",110));
        List<Apple> applelist = filterApple(list,apple -> apple.getName().equals("蘋果"));
        System.out.println(applelist);
        List<Apple> appleList = filterWeight(list, (x) -> x > 110);
        System.out.println(appleList);
    }
}


執(zhí)行結(jié)果:

[Apple{name='蘋果', weight=120}]
[Apple{name='蘋果', weight=120}]

#Lambda表達式的方法推導(dǎo)

package LambdaUse;

import java.util.function.Consumer;

/**
 * @author zhangwenlong
 * @date 2019/8/25 16:02
 */
public class MethodReference {

    private static <T> void useConsumer(Consumer<T> consumer,T t){
        consumer.accept(t);
    }

    public static void main(String[] args) {
        //定義一個匿名類
        Consumer<String> stringConsumer = (s) -> System.out.println(s);
        useConsumer(stringConsumer,"Hello World");
        //lambda
        useConsumer((s) -> System.out.println(s),"ni hao");
        //Lambda的方法推導(dǎo)
        useConsumer(System.out::println,"da jia hao");
    }
}

執(zhí)行結(jié)果:

Hello World
ni hao
da jia hao

參數(shù)推導(dǎo)其他例子:

package LambdaUse;

import java.net.Inet4Address;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * @author zhangwenlong
 * @date 2019/8/25 16:53
 */
public class paramterMethod {

    public static void main(String[] args) {
        //情況1:A method reference to a static method (for example, the method parseInt of Integer, written Integer::parseInt)
        //靜態(tài)方法
        //常用的使用
        Integer value = Integer.parseInt("123");
        System.out.println(value);
        //使用方法推導(dǎo)
        Function<String,Integer> stringIntegerFunction = Integer::parseInt;
        Integer apply = stringIntegerFunction.apply("123");
        System.out.println(apply);
        //情況2:A method reference to an instance method of an arbitrary type (for example, the method length of a String, written String::length)
        //對象的方法
        String ss = "helloWorld";
        System.out.println(ss.charAt(3));
        //推導(dǎo)方法
        BiFunction<String,Integer,Character> stringIntegerCharacterBiFunction = String::charAt;
        System.out.println(stringIntegerCharacterBiFunction.apply(ss,3));

        //情況3:構(gòu)造函數(shù)方法推導(dǎo)
        //常用方式
        Apple apple = new Apple("蘋果",120);
        System.out.println(apple);
        //推導(dǎo)方式
        BiFunction<String,Integer,Apple> appleBiFunction = Apple::new;
        System.out.println(appleBiFunction.apply("蘋果",120));
    }
}

執(zhí)行結(jié)果:

123
123
l
l
Apple{name='蘋果', weight=120}
Apple{name='蘋果', weight=120}

具體的詳細解釋可以參考:

方法推導(dǎo)詳解

#Stream(流以及一些常用的方法)
特點:并行處理
例子:

package StreamUse;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangwenlong
 * @date 2019/8/25 17:20
 */
public class StreamTest {

    public static void main(String[] args) {
        List<Apple> appleList = Arrays.asList(
                new Apple("蘋果",110),
                new Apple("桃子",120),
                new Apple("荔枝",130),
                new Apple("香蕉",140),
                new Apple("火龍果",150),
                new Apple("芒果",160)
        );
        System.out.println(getNamesByCollection(appleList));
        System.out.println(getNamesByStream(appleList));
    }

    //collection實現(xiàn)查詢重量小于140的水果的名稱
    private static List<String> getNamesByCollection(List<Apple> appleList){
        List<Apple> apples = new ArrayList<>();

        //查詢重量小于140的水果
        for(Apple apple : appleList){
            if(apple.getWeight() < 140){
               apples.add(apple);
            }
        }
        //排序
        Collections.sort(apples,(a,b)->Integer.compare(a.getWeight(),b.getWeight()));

        List<String> appleNamesList = new ArrayList<>();
        for(Apple apple : apples){
            appleNamesList.add(apple.getName());
        }
        return  appleNamesList;
    }

    //stream實現(xiàn)查詢重量小于140的水果的名稱
    private static List<String> getNamesByStream(List<Apple> appleList){
        return  appleList.stream().filter(d ->d.getWeight() < 140)
                .sorted(Comparator.comparing(Apple::getWeight))
                .map(Apple::getName)
                .collect(Collectors.toList());
    }
}

來源:https://www./content-4-425151.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    国产乱人伦精品一区二区三区四区 | 99热九九在线中文字幕| 国产又粗又猛又大爽又黄同志| 国产一区二区三中文字幕| 中文字幕亚洲人妻在线视频| 亚洲清纯一区二区三区| 欧美亚洲美女资源国产| 人妻亚洲一区二区三区| 日韩欧美国产精品自拍| 日韩精品视频免费观看| 99久久国产综合精品二区| 亚洲一区二区福利在线| 亚洲av在线视频一区| 日韩少妇人妻中文字幕| 人妻一区二区三区在线| 欧美日韩精品视频在线| 福利一区二区视频在线| 五月激情五月天综合网| 国产精品免费福利在线| 福利一区二区视频在线| 在线免费视频你懂的观看| 色鬼综合久久鬼色88| 日本欧美三级中文字幕| 伊人网免费在线观看高清版| 韩国激情野战视频在线播放| 国产老熟女超碰一区二区三区| 91欧美亚洲视频在线| 国产日韩久久精品一区| 激情亚洲一区国产精品久久| 国产精品一区二区成人在线| 亚洲精品美女三级完整版视频| 国产成人精品视频一二区| 深夜福利欲求不满的人妻| 夫妻激情视频一区二区三区| 欧美一区二区三区不卡高清视| 五月综合激情婷婷丁香| 精品国自产拍天天青青草原| 国产精品日本女优在线观看| 精品一区二区三区免费看| 亚洲男人的天堂久久a| 久久热在线免费视频精品|