Java 8 是 Java 编程语言的一个重要版本,引入了许多更新和改进。

  • Lambda表达式:

    引入了函数式编程的概念,可以简化代码,提高代码的可读性和可维护性。

1 // 使用Lambda表达式实现一个简单的线程
2 Thread thread = new Thread(() -> System.out.println("Hello, Lambda!"));
3 thread.start();

 

  • Stream API:

    提供了一种流式处理数据的方式,可以方便地进行集合操作,如过滤、映射、排序等。

1 // 使用Stream API对集合进行筛选和排序
2 List<String> fruits = Arrays.asList("apple", "banana", "cherry", "durian", "elderberry");
3 List<String> filteredFruits = fruits.stream()
4                                     .filter(fruit -> fruit.startsWith("a"))
5                                     .sorted()
6                                     .collect(Collectors.toList());
7 System.out.println(filteredFruits); // 输出:[apple]

 

  • 接口的默认方法和静态方法:

    接口可以定义默认实现的方法和静态方法,使得接口的演化更加灵活。    

 1 // 定义一个接口
 2 interface Greeting {
 3     default void sayHello() {
 4         System.out.println("Hello, World!");
 5     }
 6     static void sayGoodbye() {
 7         System.out.println("Goodbye, World!");
 8     }
 9 }
10 
11 // 实现接口并调用默认方法和静态方法
12 class GreetingImpl implements Greeting {
13     // 不需要实现sayHello方法,直接调用默认实现
14 }
15 
16 public class Main {
17     public static void main(String[] args) {
18         Greeting greeting = new GreetingImpl();
19         greeting.sayHello(); // 输出:Hello, World!
20         Greeting.sayGoodbye(); // 输出:Goodbye, World!
21     }
22 }

 

  • 方法引用:

    可以通过方法引用来直接使用已经存在的方法,简化代码。

 1 // 定义一个函数式接口
 2 interface Calculator {
 3     int calculate(int a, int b);
 4 }
 5 
 6 // 使用方法引用调用已经存在的方法
 7 public class Main {
 8     public static int add(int a, int b) {
 9         return a + b;
10     }
11 
12     public static void main(String[] args) {
13         Calculator calculator = Main::add;
14         int result = calculator.calculate(2, 3);
15         System.out.println(result); // 输出:5
16     }
17 }

 

  • Optional类:

    用于处理可能为空的值,避免了NullPointerException的问题。

 1 import java.util.Optional;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         String name = "John";
 6         Optional<String> optionalName = Optional.ofNullable(name);
 7         
 8         // 如果name不为空,则打印name的长度
 9         optionalName.ifPresent(n -> System.out.println(n.length()));
10         
11         // 如果name为空,则使用默认值"Unknown"
12         String result = optionalName.orElse("Unknown");
13         System.out.println(result); // 输出:John
14         
15         // 使用map对name进行转换
16         Optional<Integer> optionalLength = optionalName.map(String::length);
17         optionalLength.ifPresent(len -> System.out.println("Length: " + len)); // 输出:Length: 4
18     }
19 }

 

  • 新的日期和时间API:

    引入了java.time包,提供了更加强大和易用的日期和时间处理功能。

 1 import java.time.LocalDate;
 2 import java.time.LocalDateTime;
 3 import java.time.format.DateTimeFormatter;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         // 获取当前日期
 8         LocalDate currentDate = LocalDate.now();
 9         System.out.println("Current Date: " + currentDate); // 输出:Current Date: 2022-01-01
10         
11         // 格式化日期
12         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
13         String formattedDate = currentDate.format(formatter);
14         System.out.println("Formatted Date: " + formattedDate); // 输出:Formatted Date: 2022/01/01
15         
16         // 获取当前日期和时间
17         LocalDateTime currentDateTime = LocalDateTime.now();
18         System.out.println("Current DateTime: " + currentDateTime); // 输出:Current DateTime: 2022-01-01T12:34:56.789
19         
20         // 解析日期字符串
21         String dateString = "2022-01-01";
22         LocalDate parsedDate = LocalDate.parse(dateString);
23         System.out.println("Parsed Date: " + parsedDate); // 输出:Parsed Date: 2022-01-01
24     }
25 }

 

这些新特性使得Java 8成为一个更现代化和强大的编程语言,提供了更多的工具和语法糖来简化开发过程,并支持更好的并发和函数式编程。