当前位置: 首页 > news >正文

Java基础关键_035_Lambda 表达式

目  录

一、引例:TreeSet 排序

1.实现 Comparable 接口

2.比较器

3.匿名内部类

4.Lambda 表达式

5.Lambda 表达式和匿名内部类的区别

二、函数式编程

 三、Lambda 表达式的使用

1.无返回值函数式接口

(1)无返回值无参数

(2)无返回值一个参数

(3)无返回值多个参数

2.有返回值函数式接口

(1)有返回值无参数

(2)有返回值一个参数

(3)有返回值多个参数

3.表达式的简化

四、四个基本函数式接口

五、Lambda 表达式的方法引用

1.实例方法引用

2.静态方法引用

3.特殊方法引用

4.构造方法引用

5.数组引用

六、Lambda 表达式在集合中的使用

1.遍历 List 集合

2.遍历 Set 集合 

3.遍历 Map 集合

4.removeIf()


一、引例:TreeSet 排序

        我们已经知道,想要对 TreeSet 集合进行排序,有两种方式 。下面先来回顾这两种方式:


1.实现 Comparable 接口

public class User implements Comparable<User> {
    String name;
    int age;

    public User(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;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(User o) {
        return this.getAge() - o.getAge();
    }
}
public class TreeSetTest {
    public static void main(String[] args) {
        User u1 = new User("张三", 20);
        User u2 = new User("李四", 33);
        User u3 = new User("王五", 6);
        User u4 = new User("赵六", 18);
        TreeSet<User> users = new TreeSet<>();
        users.add(u1);
        users.add(u2);
        users.add(u3);
        users.add(u4);
        for (User user : users) {
            System.out.println(user);
        }
    }
}


2.比较器

public class User {
    String name;
    int age;

    public User(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;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Comparer implements Comparator<User> {
    @Override
    public int compare(User o1, User o2) {
        return o1.getAge() - o2.getAge();
    }
}
public class TreeSetTest {
    public static void main(String[] args) {
        User u1 = new User("张三", 20);
        User u2 = new User("李四", 33);
        User u3 = new User("王五", 6);
        User u4 = new User("赵六", 18);
        TreeSet<User> users = new TreeSet<>(new Comparer());
        users.add(u1);
        users.add(u2);
        users.add(u3);
        users.add(u4);
        for (User user : users) {
            System.out.println(user);
        }
    }
}

3.匿名内部类

        对 TreeSet 集合排序,除了上述两种方法外,还可以使用匿名内部类的方法。

public class InnerTest {
    public static void main(String[] args) {
        User u1 = new User("张三", 20);
        User u2 = new User("李四", 33);
        User u3 = new User("王五", 6);
        User u4 = new User("赵六", 18);
        TreeSet<User> treeSet = new TreeSet<>(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        treeSet.add(u1);
        treeSet.add(u2);
        treeSet.add(u3);
        treeSet.add(u4);
        for (User user : treeSet) {
            System.out.println(user);
        }
    }
}

4.Lambda 表达式

        匿名内部类的方式有时可以使用 Lambda 表达式的方式来编写。那么,应该怎么改写呢?

public class LambdaTest {
    public static void main(String[] args) {
        User u1 = new User("张三", 20);
        User u2 = new User("李四", 33);
        User u3 = new User("王五", 6);
        User u4 = new User("赵六", 18);

        // lambda表达式
//        TreeSet<User> treeSet = new TreeSet<>((User o1, User o2) -> {
//            return o1.getAge() - o2.getAge();
//        });

        // 简化 lambda表达式
        TreeSet<User> treeSet = new TreeSet<>((o1, o2) -> o1.getAge() - o2.getAge());

        treeSet.add(u1);
        treeSet.add(u2);
        treeSet.add(u3);
        treeSet.add(u4);
        for (User user : treeSet) {
            System.out.println(user);
        }
    }
}
  1. 编译器检测 TreeSet 构造方法,发现其参数是 Comparator 接口类型,所以明白这个方法是 Comparator 接口里的方法;

    public TreeSet(Comparator<? super E> comparator) {
        this(new TreeMap<>(comparator));
    }

  2. 限制:使用 Lambda 表达式,必须保证该接口只能有一个抽象方法


5.Lambda 表达式和匿名内部类的区别

  1. 所需类型
    1. 匿名内部类:可以是 接口、抽象类、具体类;
    2. Lambda 表达式:只能是接口。
  2. 使用限制
    1. 接口中有且仅有一个抽象方法,两者皆可使用;
    2. 接口中有多个抽象方法,不可以使用 Lambda 表达式。
  3. 原理
    1. 匿名内部类:编译后会生成一个单独的字节码文件;
    2. Lambda 表达式:编译后不会生成单独字节码文件。

二、函数式编程

  1. Java 在诞生之初,就提倡 OOP ,即面向对象编程。但随着众多新的编程语言崛起和挑战,Java 也做出了相应的调整,所以目前 Java 不但支持 OOP ,也支持 OOF ,即面向函数编程;
  2. 函数式编程重视结果,不重视过程;
  3. 函数式接口:一个接口中有且仅有一个抽象方法。

 三、Lambda 表达式的使用

1.无返回值函数式接口

(1)无返回值无参数

@FunctionalInterface
public interface Doable {
    void doSomething();
}
public class NoRetNoPara {
    public static void main(String[] args) {
        Doable doable = () -> {
            System.out.println("No Return No Parameter doSomething");
        };
        doable.doSomething();   // No Return No Parameter doSomething
    }
}

(2)无返回值一个参数

@FunctionalInterface
public interface Doable {
    void doSomething(String str);
}
public class NoRetOnePara {
    public static void main(String[] args) {
        Doable doable = (String name) -> {
            System.out.println("No Return One Parameter doSomething: " + name);
        };
        doable.doSomething("Lambda");   // No Return One Parameter doSomething: Lambda
    }
}

(3)无返回值多个参数

@FunctionalInterface
public interface Doable {
    void doSomething(String str, int i);
}
public class NoRetMorePara {
    public static void main(String[] args) {
        Doable doable = (String str, int i) -> {
            System.out.println("No Return More Parameter:" + str + i);
        };
        doable.doSomething("hello", 1);    // No Return More Parameter:hello1
    }
}

2.有返回值函数式接口

(1)有返回值无参数

@FunctionalInterface
public interface Doable {
    String doSomething();
}
public class HaveRetNoPara {
    public static void main(String[] args) {
        Doable doable = () -> {
            return "Hava Return No Parameter doSomething";
        };
        System.out.println(doable.doSomething());   // Hava Return No Parameter doSomething
    }
}

(2)有返回值一个参数

@FunctionalInterface
public interface Doable {
    String doSomething(String str);
}
public class HaveRetNoPara {
    public static void main(String[] args) {
        Doable doable = (name) -> {
            return "Hava Return No Parameter doSomething:" + name;
        };
        System.out.println(doable.doSomething("Lambda"));   // Hava Return No Parameter doSomething:Lambda
    }
}

(3)有返回值多个参数

@FunctionalInterface
public interface Doable {
    String doSomething(String str, int i);
}
public class HaveRetNoPara {
    public static void main(String[] args) {
        Doable doable = (name, i) -> {
            return "Hava Return No Parameter doSomething:" + name + i;
        };
        System.out.println(doable.doSomething("Lambda", 1));   // Hava Return No Parameter doSomething:Lambda1
    }
}

3.表达式的简化

  1. 形参类型可省略,若省略则每个形参类型都需要省略;
  2. 若形参列表只有一个参数,则形参类型和小括号都可以省略;
  3. 若方法体只有一条语句,则方法体的大括号可以省略;
  4. 若方法体只有一条 return 语句,则大括号可以省略,若省略大括号,则必须省略 return 关键字。

四、四个基本函数式接口

接口名抽象方法
Consumer<T>void accept(T t)
Supplier<T>T get()
Function<T, R>R apply(T t)
Predicate<T>boolean test(T t)

        这些接口都在 java.util.function 下,通常函数接口出现的地方都可以使用 Lambda 表达式。


五、Lambda 表达式的方法引用

1.实例方法引用

  1. 语法:【对象::实例方法】;
  2. 特点:在 Lambda 表达式的方法体中,通过“对象”来调用指定的某个“实例方法”;
  3. 要求:函数式接口中抽象方法的返回值类型、参数列表 与 内部通过对象调用某个实例方法的返回值类型、形参列表 保持一致。
public class Person {
    private String name;

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}
public class InstanceMethod {
    public static void main(String[] args) {
        Person person = new Person("小明");
        // lambda 表达式,此处使用 supplier 接口
        Supplier supplier1 = () -> person.getName();
        System.out.println(supplier1.get());    // 小明

        // 方法引用
        Supplier supplier2 = person::getName;
        System.out.println(supplier2.get());    // 小明
    }
}

2.静态方法引用

  1. 语法:【类::静态方法】;
  2. 特点:在 Lambda 表达式的方法体中,通过“对象”来调用指定的某个“静态方法”;
  3. 要求:函数式接口中抽象方法的返回值类型、参数列表 与 内部通过对象调用某个静态方法的返回值类型、形参列表 保持一致。
public class StaticMethod {
    public static void main(String[] args) {
        // lambda 表达式,此处使用 function 接口
        Function<Double, Long> function = value -> Math.round(value);
        System.out.println(function.apply(3.14));   // 3

        // 静态方法引用
        Function<Double, Long> function2 = Math::round;
        System.out.println(function2.apply(3.14));  // 3
    }
}

3.特殊方法引用

  1. 语法:【类::实例方法】;
  2. 特点:在 Lambda 表达式的方法体中,通过“方法的第一个形参”来调用指定的某个“实例方法”;
  3. 要求:函数式接口中抽象方法的 第一个形参作为方法的调用者对象,从第二个形参开始(或无参)可以对应到被调用的实例方法的参数列表,且返回值类型 保持一致。
public class SpecialMethod {
    public static void main(String[] args) {
        // lambda 表达式,此处使用 comparator 接口
        Comparator<Integer> comparator = (x, y) -> x.compareTo(y);
        System.out.println(comparator.compare(3, 1)); // 1

        // 特殊方法引用
        Comparator<Integer> comparator2 = Integer::compareTo;
        System.out.println(comparator2.compare(3, 1));  // 1
    }
}

4.构造方法引用

  1. 语法:【类::new】;
  2. 特点:在 Lambda 表达式的方法体中,返回指定“类名”创建出来的对象;
  3. 要求:创建对象所调用的构造方法的形参列表 和 函数式接口中方法的形参列表 保持一致,且方法返回值类型和创建对象的类型一致。
public class Person {
    private String name;

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

    public Person() {
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}
public class ConstructMethod {
    public static void main(String[] args) {
        // lambda 表达式,此处使用 supplier 接口
        Supplier<Person> supplier = () -> new Person();
        System.out.println(supplier.get()); // Person{name='null'}

        // 构造方法引用
        Supplier<Person> supplier2 = Person::new;
        System.out.println(supplier2.get());    // Person{name='null'}
    }
}

5.数组引用

  1. 语法:【数组类型::new】;
  2. 特点:在 Lambda 表达式的方法体中,创建并返回指定类型的数组;
  3. 要求:重写的方法有且仅有一个整数型参数,且该参数用于设置数组的长度,重写方法的返回值类型和创建数组类型保持一致。
public class ArrayTest {
    public static void main(String[] args) {
        // lambda 表达式,此处使用 Function 接口
        Function<Integer, int[]> function = length -> new int[length];
        int[] apply = function.apply(3);
        System.out.println(Arrays.toString(apply)); // [0, 0, 0]

        // 数组构造方法引用
        Function<Integer, int[]> function2 = int[]::new;
        int[] apply2 = function2.apply(5);
        System.out.println(Arrays.toString(apply2));    // [0, 0, 0, 0, 0]
    }
}

六、Lambda 表达式在集合中的使用

1.遍历 List 集合

public class ListTest {
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 31, 25, 77, 9, 66);
        // lambda 表达式
        list.forEach(x -> System.out.print(x + "\t"));  // 1	31	25	77	9	66

        System.out.println();

        // 方法引用
        list.forEach(System.out::print);    // 1312577966
    }
}

2.遍历 Set 集合 

public class TreeSetTest {
    public static void main(String[] args) {
        TreeSet<String> treeSet = new TreeSet<>();
        treeSet.add("小明");
        treeSet.add("小红");
        treeSet.add("小刚");

        // lambda 表达式
        treeSet.forEach((str) -> System.out.print(str));    // 小刚小明小红

        System.out.println();

        // 方法引用
        treeSet.forEach(System.out::print); // 小刚小明小红
    }
}

3.遍历 Map 集合

public class MapTest {
    public static void main(String[] args) {
        Map<String, String> map = Map.of("key1", "value1", "key2", "value2", "key3", "value3");
        // lambda 表达式
        map.forEach((key, value) -> System.out.print(key + ":" + value + "\t"));    // key3:value3	key2:value2	key1:value1	
        // 方法引用不能用
    }
}

4.removeIf()

        集合的 removeIf 方法可以与 Lambda 表达式结合,以实现删除符合条件的元素。使用的函数式接口应该是判断型,即返回 boolean 类型的方法。

public class RemoveIfTest {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("《大学》");
        arrayList.add("《中庸》");
        arrayList.add("《论语》");
        arrayList.add("《孟子》");
        arrayList.add("《春秋》");

//        arrayList.removeIf(s -> s.contains("春秋"));
        arrayList.removeIf("《春秋》"::equals);
        System.out.println(arrayList);  // [《大学》, 《中庸》, 《论语》, 《孟子》]
    }
}

相关文章:

  • 【特权FPGA】之AT24C02 IIC实现
  • 光流 | 近十年(2015–2025年)光流算法研究的代表性文献综述及光流开源项目
  • Langchat平台知识库测试
  • MySQL--基础知识点--81.2--EVENT
  • Spark core编程之RDD 转换算子分类
  • java并发编程面试之ThreadLocal深度解析
  • python多线程+异步编程让你的程序运行更快
  • 蓝桥杯 分解质因数(唯一分解定理)
  • 【专题】贪心算法
  • [C语言]gets和fgets函数区别及详解
  • uniapp微信小程序图片生成水印
  • scrum详细理解
  • 前端笔记-JavaScript部分(上)
  • [特殊字符]深入浅出理解 URL:从新手到精通的系统解析
  • App Cleaner Pro for Mac 中 Mac软件卸载工具
  • 10 个最新 CSS 功能已在所有主流浏览器中得到支持
  • 软件设计师-下午题-试题1(15分)
  • Excel 动态比较两列数据:实现灵活的数据验证
  • minio命令行客户端mc常见用法
  • 活动策划岗位(应届生求职)
  • 网站建设做网站/保定关键词优化软件
  • 自己做的网站突然打不开/杭州优化seo公司
  • 淮北市住房和城乡建设局网站/有友情链接的网站
  • 怎么弄网站/王通seo赚钱培训
  • 建设局官方网站/百度seo代理
  • 做什么网站赚钱/互联网运营推广公司