目录10 jdk1.8新功能10.1 Lambda 表达式10.2 函数式接口Functional Interface10.3 Stream API10.4 默认方法Default Method10.5 Optional10.6 日期时间 API (java.time 包)10.6 方法引用Method Reference10.8 重复注解Repeatable Annotations10 jdk1.8新功能JDK 1.8 就是 Java 8。Java 8 是 Java 语言发展史上一次非常重要的版本更新2014年主要引入了 函数式编程思路和许多提升开发效率的特性不仅改进了语法还带来了更强大的类库。主要的核心新功能一共有8个。10.1 Lambda 表达式解释Lambda 允许将函数作为参数传递支持函数式编程简化匿名内部类的写法。语法(parameters) - expression 或 (parameters) - { statements; }示例用 Lambda 表达式实现 Runnable 接口// 传统方式 new Thread(new Runnable() { Override public void run() { System.out.println(Hello World); } }).start(); // Lambda 方式 new Thread(() - System.out.println(Hello Lambda)).start();10.2 函数式接口Functional Interface解释只包含一个抽象方法的接口可用 FunctionalInterface 注解标识。Java 8 内置了常用函数式接口Predicate: 接收 T 并返回 booleanConsumer: 接收 T 不返回结果Function: 接收 T 返回 RSupplier: 无输入返回 T示例import java.util.function.Predicate; public class Test { public static void main(String[] args) { PredicateString isEmpty s - s.isEmpty(); System.out.println(isEmpty.test()); // true } }10.3 Stream API解释对集合或数组进行声明式、链式操作支持并行处理提升可读性和性能。核心方法filter(): 过滤元素map(): 转换元素forEach(): 遍历元素collect(): 收集结果sorted(): 排序示例import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Test { public static void main(String[] args) { ListString names Arrays.asList(Alice, Bob, Charles); // 过滤长度大于3的名字并转换为大写 ListString filtered names.stream() .filter(name - name.length() 3) .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println(filtered); // [ALICE, CHARLES] } }10.4 默认方法Default Method解释接口中可以定义带有实现的方法用 default 关键字修饰避免在增加新方法时破坏已有实现类。示例interface MyInterface { default void sayHello() { System.out.println(Hello from interface); } } class MyClass implements MyInterface { public static void main(String[] args) { MyClass obj new MyClass(); obj.sayHello(); // Hello from interface } }10.5 Optional解释用来优雅地处理可能为 null 的值避免频繁的 null 判断防止空指针异常。示例import java.util.Optional; public class Test { public static void main(String[] args) { String name John; OptionalString optionalName Optional.ofNullable(name); optionalName.ifPresent(System.out::println); // John String defaultName optionalName.orElse(Unknown); System.out.println(defaultName); // John } }10.6 日期时间 API (java.time 包)解释全新的不可变日期时间类替代 java.util.Date 和 java.util.Calendar提供更友好的 API。示例import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Test { public static void main(String[] args) { LocalDate today LocalDate.now(); System.out.println(today); // 2024-05-20 LocalDateTime currentTime LocalDateTime.now(); DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss); String formatted currentTime.format(formatter); System.out.println(formatted); // 2024-05-20 14:30:00 } }10.6 方法引用Method Reference解释在 Lambda 表达式中直接引用已有方法简化代码。四种引用方式对象::实例方法类::静态方法类::实例方法类构造器引用(ClassName::new)示例import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] args) { ListString names Arrays.asList(Alice, Bob); names.forEach(System.out::println); // 方法引用 } }10.8 重复注解Repeatable Annotations解释一个地方可以使用同一个注解多次。示例import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; Repeatable(Authors.class) interface Author { String name(); } Retention(RetentionPolicy.RUNTIME) interface Authors { Author[] value(); } Author(name Alice) Author(name Bob) public class Book { }