Interview to kill | Please talk about the new features introduced by Java8-18 (1)

Get into the habit of writing together! This is the sixth day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Java8 was released on March 18, 2014, and as of April 6, 2022, the latest release is Java18. Versions 17, 11 and 8 are the currently supported Long Term Support (LTS) releases. This article leads you to review the features of each version starting from Java 8. Sit on the bench and go!

Version overview

The last free public update for Java8 LTS for commercial use was released by Oracle in January 2019, and Oracle continues to update and release free public java8 for development and personal use. java7 is no longer supported by the public. For java11, Oracle will not provide long-term support for the public; instead, the wider OpenJDK community, such as the Eclipse Adoptium or others, will provide such support in place of Oracle.

image.png

The figure above shows the historical version release process from Java SE 8 to Java SE 18. In September 2017, Mark Reinhold, the chief architect of the Java platform, proposed to change the release train to " a feature release every six months ". The agreement also gives the release plan for Java SE 21 in the future.

The general availability of Java 18 starts on March 22, 2022, and the latest (third) LTS Java 17 starts on September 14, 2021. java19 is under development and early access builds are already available.

Java 8 features

Lambda expressions allow us to use functions as method parameters.

Let's look at the code before Java 8, when we had to create an anonymous class to implement a simple interface.

Thread t = new Thread(new Runnable() {
 public void run() {
  System.out.println("Start thread");
 }
});
复制代码

Using lambda expressions, we can do this.

Thread t1 = new Thread(() -> {
 System.out.println("Start 1st thread");
});
复制代码

A functional interface is an interface that has only one abstract method. Also, an Interface can have default methods and static methods, but only one abstract method.

Streamjava.util.Stream包中的一个接口,它提供了对集合执行顺序和并行操作的方法。Collection Interface 的 Stream ()方法返回给定集合的类型为 Stream 的元素流。流接口支持过滤、映射或查找流中元素的聚合结果所需的许多操作。

在下面的实例,我们调用 List 上的 stream ()方法,然后将 Predicate (Functional Interface)传递给返回布尔值的 anyMatch ()方法。

List<String> colors = new ArrayList<>(Arrays.asList("Green","Yellow","Red"));
boolean isTrue = colors.stream().anyMatch(x -> x.equals("Red"));
System.out.println(isTrue);
复制代码

Optional 可创建一个容器对象,该对象可能包含也可能不包含非空值,通过使用 它,我们可以isPresent ()来检查一个值是否为非空。

在下面的示例中,我们使用 Stream Interface 的 findAny ()方法返回一个 Optional 对象。基于可选实例的值,我们可以执行相应的逻辑。

List<String> colors = new ArrayList<>(Arrays.asList("Green","Yellow","Red"));
Optional<String> color = colors.stream().filter(x -> x.equals("Black")).findAny();
System.out.println(color.isPresent()? color.get() : "Not Found");
复制代码

java.util.concurrent 包的更改:

  • 引入了 Interface CompletionStage、 Class CompletableFuture 和 CompletionException。
  • CompletableFuture 可以帮助执行异步任务,这些任务比在 java8之前存在的 Future 对象更加灵活和增强。

Method References (::) ,我们可以使用双冒号来调用现有的方法,而不是使用 lambda 表达式。方法引用可以用来引用静态方法和实例方法。

下面演示一个引用静态方法的示例。首先,让我们使用 lambda 表达式实现一个方法调用。

public class Main {
    public static void main(String[] args)  {
     List<Integer> locList = new ArrayList<>(Arrays.asList(1,2,5,6));
     boolean isFound = locList.stream().anyMatch(l -> isEqual(l));
     System.out.println(isFound);
    }
    public static <T> boolean isEqual(T a) {
    Predicate<T> isEqual = (x) -> x.equals(5);
    return isEqual.test(a);
    }
}
复制代码

isEqual() is a static method in the Main class, so instead of calling the method with a lambda expression, we can simply use a method reference to do the work.

public class Main {
    public static void main(String[] args)  {
     List<Integer> locList = new ArrayList<>(Arrays.asList(1,2,5,6));
     boolean isFound = locList.stream().anyMatch(Main::isEqual);
     System.out.println(isFound);
    }
    public static <T> boolean isEqual(T a) {
     Predicate<T> isEqual = (x) -> x.equals(5);
     return isEqual.test(a);
    }
}
复制代码

Due to limited space, in addition to the new features mentioned above, there are several important ones, including and not limited to:

  • Parallel Array Sorting
  • Date & Time API (date and time operation interface, LocalDate, ZoneId, Clock)
  • Base64 encoding and decoding (Base64 encoding and decoding is self-supporting)
  • Permanent Generation (Metaspace will rescale itself based on our needs at runtime)

To be continued, let's continue to talk about the new features of each version, so stay tuned!

Guess you like

Origin juejin.im/post/7083284455502118948