Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • Java语法

  • Java容器

  • Java新特性

    • Java8

      • 函数式接口
      • 方法引用
      • Stream流
      • Lambda表达式
        • 示例实现
          • Thread
          • ArrayList
          • Map
        • 函数式接口中的实现
          • 无参数
          • 一个参数
          • 多个参数
        • 实现原理
  • IDEA常用快捷键
  • 正则表达式
  • API
  • 场景题

  • JavaSE
  • Java新特性
  • Java8
Nreal
2023-12-23
目录

Lambda表达式

针对匿名内部类的写法简化;

(参数列表)->{代码}
1

# 示例实现

# Thread

在创建线程并启动时可以使用匿名内部类的写法:

new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("你知道吗 我比你想象的 更想在你身边");
    }
}).start();
1
2
3
4
5
6

可以使用Lambda的格式(只关注参数和方法体)对其进行修改。修改后如下:

new Thread(()->{
    System.out.println("你知道吗 我比你想象的 更想在你身边");
}).start();
1
2
3

# ArrayList

ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);

list.forEach(n -> {
    if (n%2 == 0) 
        System.out.println(n);
});
1
2
3
4
5
6
7
8
9

# Map

Map<String,Integer> map = new HashMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.forEach((k,v)->System.out.println(k+v));
1
2
3
4
5

# 函数式接口中的实现

函数式接口:只包含一个抽象的(未实现)方法的接口;还可以包含具有实现的默认方法和静态方法;

# 无参数

public class E1 {
    public interface MyFunctionalInterface1 {
        public String test();
    }

    public static void main(String[] args) {
        MyFunctionalInterface1 m = ()->{
            return "test";
        };
        System.out.println(m.test());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 一个参数

public class E2 {
    public interface MyFunctionalInterface2{
        public String test(String s);
    }

    public static void main(String[] args) {
        MyFunctionalInterface2 m = (s)->{
            return "hello "+s;
        };
        System.out.println(m.test("Nreal"));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 多个参数

public class E3 {
    public interface MyFunc3{
        public String test(String s1,String s2);
    }

    public static void main(String[] args) {
        MyFunc3 m = (s1,s2)->{
            return s1+s2;
        };
        System.out.println(m.test("hello ","Nreal"));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 实现原理

Lambda 表达式的实现原理涉及到 invokedynamic 指令和 LambdaMetafactory 类。在运行时,Lambda 表达式会被转换成 invokedynamic 指令的调用,然后通过 LambdaMetafactory 类动态地创建函数式接口的实例;

Stream流
IDEA常用快捷键

← Stream流 IDEA常用快捷键→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式