马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Java Lambda
Lambda 表达式是 Java 8 的焦点特性,通过 函数式编程 大幅简化代码。其焦点思想是将行为作为参数传递,替换匿名内部类,提升代码的简洁性和可读性。以下是系统解析和完整代码示例:
一、Lambda 表达式基础
- 语法结构
- 无参数:() -> System.out.println("Hello")
- 单参数:s -> System.out.println(s)(括号可省略)
- 多参数:(a, b) -> a + b
- 代码块:多行逻辑需用 {} 包裹,显式利用 return。
- 本质
Lambda 是 函数式接口(单抽象方法的接口) 的实例,编译时主动转换为接口实现。
- @FunctionalInterface
- interface MyFunction {
- int apply(int a, int b);
- }
- MyFunction add = (x, y) -> x + y; // Lambda 实现
复制代码 二、焦点应用场景与代码示例
1. 替换匿名内部类
- // 传统匿名内部类
- Runnable oldRunnable = new Runnable() {
- @Override
- public void run() {
- System.out.println("Running");
- }
- };
- // Lambda 简化
- Runnable newRunnable = () -> System.out.println("Running");
复制代码 2. 聚集操纵(Stream API)
- List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
- // 遍历
- names.forEach(name -> System.out.println(name)); // 输出每个名字
- // 过滤 + 映射
- List<String> result = names.stream()
- .filter(name -> name.startsWith("A")) // 过滤以 A 开头的名字
- .map(String::toUpperCase) // 转为大写
- .collect(Collectors.toList()); // 收集为 List
- // 结果: ["ALICE"]
- // 排序
- names.sort((s1, s2) -> s1.compareTo(s2)); // 字典序排序
复制代码 3. 函数式接口与内置工具
- // Predicate(条件判断)
- Predicate<String> isLong = s -> s.length() > 3;
- System.out.println(isLong.test("Java")); // 输出 true
- // Consumer(消费数据)
- Consumer<String> printer = s -> System.out.println("Consumed: " + s);
- printer.accept("Hello"); // 输出 "Consumed: Hello"
- // Function(数据转换)
- Function<Integer, String> intToStr = num -> "Number: " + num;
- System.out.println(intToStr.apply(10)); // 输出 "Number: 10"
复制代码 4. 多线程与事件处理
- // 线程启动
- new Thread(() -> System.out.println("Thread running")).start();
- // 按钮事件(Swing)
- JButton button = new JButton("Click");
- button.addActionListener(e -> System.out.println("Button clicked"));
复制代码 三、注意事项
- 变量捕获
Lambda 可访问 final 或等效不可变的局部变量(隐式 final)。
- int base = 10;
- Function<Integer, Integer> adder = x -> x + base; // base 需不可变
复制代码 - 类型推断
参数类型可省略,编译器主动推断:
- BinaryOperator<Integer> add = (a, b) -> a + b; // 无需写 (Integer a, Integer b)
复制代码 - 方法引用
进一步简化 Lambda:
- names.forEach(System.out::println); // 等效于 name -> System.out.println(name)
复制代码 四、完整示例代码
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.*;
- public class LambdaDemo {
- public static void main(String[] args) {
- // 1. 函数式接口实现
- MyFunction multiply = (a, b) -> a * b;
- System.out.println("5 * 3 = " + multiply.apply(5, 3)); // 输出 15
- // 2. 集合操作
- List<String> languages = Arrays.asList("Java", "Python", "C++", "Go");
- List<String> filtered = languages.stream()
- .filter(lang -> lang.length() > 3)
- .map(String::toUpperCase)
- .collect(Collectors.toList());
- System.out.println(filtered); // 输出 [JAVA, PYTHON]
- // 3. 多线程
- new Thread(() -> {
- for (int i = 0; i < 3; i++) {
- System.out.println("Thread: " + i);
- }
- }).start();
- }
- @FunctionalInterface
- interface MyFunction {
- int apply(int a, int b);
- }
- }
复制代码 输出结果:
- 5 * 3 = 15
- [JAVA, PYTHON]
- Thread: 0
- Thread: 1
- Thread: 2
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|