lambda常見用法

        //老的寫法
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World");
            }
        }).run();

       //新的寫法
        new Thread(
                () -> System.out.println("lambda:hello world")
        ).run();

1. -> 替代了 類名、方法名 前提:函數(shù)接口

/**
 * 函數(shù)式接口是指內(nèi)部只有一個(gè)抽象方法的接口
 */
public interface MyInterface {
    int add(int a ,int b);
}
//老的寫法
MyInterface myInterface1 = new MyInterface() {
            @Override
            public int add(int a, int b) {
                return a + b;
            }
        };
 //新的寫法,括號(hào)里面是參數(shù)
 MyInterface myInterface2 = (a, b) -> a+b;

2. 在集合中的應(yīng)用

       // 在 ArrayList的應(yīng)用
        List<Integer> idList = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));
        idList.forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer i) {
                System.out.println(i);
            }
        });

        idList.forEach((i)-> System.out.println("lambda:"+i));

        //Map 中的應(yīng)用
        Map<String,String> map = new HashMap<>();
        map.put("a","1");
        map.put("b","2");
        map.put("c","3");
        map.forEach((k,v)-> System.out.println("lambda: k:"+k + "  v:"+v));

3.stream()

@Data
public class Student {

    private String name;
    private String sex;

    public Student(String name,String sex){
        this.name = name;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return name + " : " + sex;
    }
}
        // 得到stream:Collection.stream()、Arrays.stream(T[] array)
        // 1.中間操作  distinct() filter()  map() 等
        // 2.結(jié)束操作 collect() forEach() 等
        Student student1 = new Student("zhao","male");
        Student student2 = new Student("qian","male");
        Student student3 = new Student("sun","male");
        Student student4 = new Student("li","female");
        List<Student> list = new ArrayList<>(Arrays.asList(student1,student2,student3,student4));

        // 比如 獲取 學(xué)生的姓名 的list   Student::getName 方法的等同于student -> {return student.getName(); })
        List<String> nameList = list.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println(nameList);

        // 比如 生成一個(gè) 學(xué)生姓名 + student的 map   Function.identity() 代表對(duì)象本身
        Map<String,Student> map =  list.stream().collect(Collectors.toMap(Student::getName, Function.identity()));
        System.out.println(map);

        // groupBy 按性別歸類學(xué)生
        Map<String,List<Student>> sexAndStudentMap = list.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(sexAndStudentMap);
        Map<String,List<String>> sexAndNameMap = list.stream().collect(Collectors.groupingBy(Student::getSex,
                Collectors.mapping(Student::getName,Collectors.toList())));
        System.out.println(sexAndNameMap);

以上是常用的一些場(chǎng)景,關(guān)于具體的一些操作,可查看原代碼上的注釋,一般都會(huì)有示例,例如:
image.png

參考資料:https://github.com/CarpenterLee/JavaLambdaInternals

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容