一、Lambda表达式
1、Lambda表达式是Java 1.8中最重要的新特性之一,它使得Java程序员可以更轻松高效地编写代码
Runnable r = () -> System.out.println("Hello World");
r.run(); // 输出"Hello World"
2、Lambda表达式使得在集合的处理中使用函数变得更加简单。
List<String> list = Arrays.asList("one", "two", "three");
list.forEach(s -> System.out.println(s));
3、Lambda表达式也在并行处理中就显得尤为重要,使得编程人员可以很便捷地并行操作实现高性能的代码。
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int sum = numbers.parallelStream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum();
System.out.println("sum=" + sum); // 输出15
二、新的日期/时间API
1、Java 1.8的日期/时间API提供了更加全面和易于使用的日期/时间操作方法。
LocalDateTime time = LocalDateTime.now();
System.out.println(time); // 输出当前日期和时间
LocalDate date = time.toLocalDate();
System.out.println(date); // 输出当前日期
2、Java 1.8还提供了另一种日期和时间API,Instant类,它表示时刻,不受时区的影响。
Instant instant = Instant.now();
System.out.println(instant); // 输出当前时刻
3、新的日期/时间API还提供了一些简单的日期和时间操作方法,如计算两个日期之间的时间差,添加指定时间单位等。
LocalDateTime time1 = LocalDateTime.now();
LocalDateTime time2 = LocalDateTime.of(2019, 9, 10, 8, 0, 0);
Duration duration = Duration.between(time1, time2);
System.out.println("Duration: " + duration.toDays() + " days");
三、接口中的新特性
1、Java 1.8中允许在接口中定义默认方法和静态方法,使得接口可以包括实现。
public interface Calculator {
default int add(int x, int y) {
return x + y;
}
static int subtract(int x, int y) {
return x - y;
}
}
2、这些新特性使得在接口中添加新功能变得更加简单,同时还可以向旧接口中添加新的默认实现而不会破坏已有代码的结构。
public interface MyInterface {
default void hello() {
System.out.println("Hello World from MyInterface");
}
}
public class MyClass implements MyInterface {
// 不需要重写hello方法
}
3、同时,这些新特性还使得匿名内部类的代码变得更加简洁。
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Hello World");
}
};
r.run();
// 用Lambda表达式替换以上代码
Runnable r2 = () -> System.out.println("Hello World");
r2.run();
四、Stream API
1、Java 1.8中引入的新Stream API提供了更加高效和便捷的方式进行集合操作,大大简化了代码。
List<String> strings = Arrays.asList("one", "two", "three", "four", "five");
long count = strings.stream().filter(s -> s.length() > 3).count();
System.out.println("The number of strings with length greater than 3: " + count);
2、Stream API还提供了许多有用的操作方法,如映射、排序、分组、拆分等。
List<String> strings = Arrays.asList("one", "two", "three", "four", "five");
// 使用map方法进行字符串转换
List<Integer> lengths = strings.stream().map(String::length).collect(Collectors.toList());
System.out.println(lengths);
3、Stream API还支持并行操作,充分利用多核处理器的特性,提高程序性能。
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int sum = numbers.parallelStream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum();
System.out.println("sum=" + sum); // 输出15
五、其他新特性
1、Java 1.8中还包括了许多其他新特性,如Type Annotations、Nashorn JavaScript引擎和Base64编码等。
// Type Annotations
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_PARAMETER)
public @interface MyAnnotation {
}
// Nashorn JavaScript引擎
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("print('Hello World from Nashorn')");
// Base64编码
String base64 = Base64.getEncoder().encodeToString("Hello World".getBytes());
System.out.println(base64); // 输出SGVsbG8gV29ybGQ=
2、JavaFX也已经成为Java SE 8的一部分,提供了许多使得桌面应用程序更加易于编写的新功能。
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(e -> System.out.println("Hello World"));
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
3、Java 1.8提供了更好的性能和安全性保障,如增量编译、元空间等。
jshell> /exit
$ java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)