Lambda表达式笔记

一.  总概述:

 

二.  概述

 面向对象中的缺陷:需要创建对象,然后使用对象,使得语句有点冗余,在jdk1.8的时候,加入了lambda表达式,即函数式编程思想。使得编程者只“关注做什么事”,而省略了一些步骤

 

三. 格式:(参数名称)->  {方法体} 

 

四. 具体案列

  1. 无参测试:

public class Main {
    public static void main(String[] args) {
        //面向对象式的创建线程!
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();

        //匿名内部类创建一个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"匿名内部类创建的线程!");
            }
        }).start();

        //lambda标准式线程
        new Thread(()-> System.out.println(Thread.currentThread().getName()+"lambda标准式创建的线程")).start();

        //lambda简洁式线程
        // 1. 由于无参数,用括号代替
        // 2. 由于方法体只有一句话,省略大括号和分号
        new Thread(()-> System.out.println(Thread.currentThread().getName()+"lambda简洁式创建的线程")).start();
    }
}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"对象创建的线程!");
    }
}

  2. 只有一个参数

public class MainTest {
    public static void main(String[] args) {
        String name = "旺财";
        //普通创建对象调用方法
        new FunctionTest(name).getFood(new Function() {
            @Override
            public void getFood(String name) {
                System.out.println(name + "得到了食物");
            }
        });
        
        //标准lumbda标准调用方法
        new FunctionTest(name).getFood((String rs)-> System.out.println(rs+"得到了食物"));
        
        //简洁式lumbda标准调用方法
        // 1. 由于只有参数,省略括号、参数数据类型。
        // 2. 由于方法体只有一句话,省略大括号和分号
        new FunctionTest(name).getFood(rs-> System.out.println(rs+"123"));

    }
}


class FunctionTest {

    private String name;

    public FunctionTest(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void getFood(Function function){
        function.getFood(name);
    }
}


interface Function<T> {
    void getFood(String name);

多个参数

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class Main {
    public static void main(String[] args) {
        //按年龄降序排列
        Student stu = new Student("tom",13);
        Student stu1 = new Student("james",15);
        Student stu2 = new Student("Linda",14);
        Student stu3 = new Student("jack",12);

        ArrayList<Student> students = new ArrayList<>();
        Collections.addAll(students,stu,stu1,stu2,stu3);

        //普通方法按年龄排序
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        for (Student student : students) {
            System.out.println(student.getName()+"->"+student.getAge());
        }
        System.out.println("========================");
        //标准式lumbda排序
        Collections.sort(students,(Student o1, Student o2)-> {
            return o2.getAge()-o1.getAge();
        });
        for (Student student : students) {
            System.out.println(student.getName()+"->"+student.getAge());
        }
        System.out.println("========================");
        //简介式lumbda
        //1.  含有两个参数,可以省略数据类型
        //2.  方法体只有一句,由于需要返回值类型,可以省略return ,大括号和分号
        Collections.sort(students,(o1, o2) -> o1.getAge()-o2.getAge());
        //集合转化为流输出
        students.stream().forEach(rs-> System.out.println(rs.getName()+"->"+rs.getAge()));

    }
}

class Student{
    private String name;
    private int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值