学习顺序:Java 基础 → Spring Boot → Vue → 前后端整合 → 数据库 → 部署 → 进阶实战。
Lambda 表达式(Lambda 表达式是 Java 8 引入的核心特性,旨在简化函数式编程,替代冗长的匿名内部类,使代码更简洁、灵活 )
Lambda 必须与函数式接口(Functional Interface) 配合使用,即接口中仅有一个抽象方法。
@FunctionalInterface//自定义函数接口interface Calculator {int operate(int a, int b);}// 使用 Lambda 实现加法Calculator add = (a, b) -> a + b;System.out.println(add.operate(3, 5)); // 输出 8
// 静态方法引用
Function<Double, Double> sqrt = Math::sqrt;
// 实例方法引用
List<String> names = Arrays.asList("Alice", "Bob");
names.forEach(System.out::println); // 等效于 s -> System.out.println(s)
// 构造器引用
Supplier<List<String>> listSupplier = ArrayList::new;
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// 过滤偶数,平方后求和
int sum = numbers.stream().filter(n -> n % 2 == 0) // Predicate.map(n -> n * n) // Function.reduce(0, Integer::sum); // BinaryOperator
System.out.println(sum); // 输出 20 (2² + 4² = 4 + 16)
// 显式类型
Comparator<Integer> cmp = (Integer a, Integer b) -> a.compareTo(b);
// 隐式类型(推荐)
Comparator<Integer> cmp = (a, b) -> a.compareTo(b);
@FunctionalInterface
interface FileProcessor {void process() throws IOException; // 声明异常
}
FileProcessor processor = () -> Files.readAllBytes(Paths.get("file.txt"));
错误堆栈中直接显示processString,明确指出问题发生在processString方法中,方便快速定位
集合排序中,a.length()-b.length()[从小到大] b.length()-a.length()[从大到小]
Optional 避免空指针、新的日期时间 API
链式处理就是.map().filter();