What are the new features of Java 8?

JavaSE 8 new features

Catalog
1, lambda expression
2, Stream api
3, javaFx
4, new date and time api
5, concurrency enhancement
6, Nashorn
7, other improvements
1, lambda expression

1.1. What is a Lambda expression? Why use lambda expressions?
Lambda expressions allow developers to write concise "calculation fragments" and pass them on to other code. Functions are allowed as parameters of a method (functions are passed as parameters into methods). Using Lambda expressions can make the code more concise and compact.

1.2. The format of the lambda expression: parameter -> an expression
Important features:
Optional type declaration: No need to declare the parameter type, the compiler can uniformly identify the parameter value.
Optional parameter parentheses: one parameter does not need to define parentheses, but multiple parameters need to define parentheses.
 Optional curly braces: If the body contains a statement, curly braces are not required.
Optional return keyword: If the body has only one expression return value, the compiler will automatically return the value, and the curly braces need to indicate that the expression returns a value.

1.3. A lambda expression includes three parts:
 A piece of code
 Parameters
 Free variables, where "free" refers to those variables that are not parameters and are not defined in the code. Code blocks containing free variables are called "closures"
Note:
Lambda expressions are mainly used to define method-type interfaces that are executed inline, for example, a simple method interface.
Lambda expressions eliminate the need for anonymous methods and give Java simple but powerful functional programming capabilities.

2.1, Stream API
Java 8 API adds a new abstraction called stream Stream, which allows you to process data in a declarative way. Stream (stream) is a queue of elements from a data source and supports aggregation operations. Think of the collection of elements to be processed as a stream, which is transmitted in a pipeline and can be processed on the nodes of the pipeline, such as filtering, sorting, aggregation, etc. The element stream is processed by intermediate operations in the pipeline, and finally the final operation obtains the results of the previous processing.

Collection interface has two methods to generate streams:
stream() − Creates a serial stream for a collection.
parallelStream() − Creates a parallel stream for a collection.

Common APIs

filter()  //过滤
map()  // 将流地元素映射成另一个类型
distinct()  // 去除流中重复地元素
sorted()  // 对元素进行排序
forEach()  // 对流中地每个元素执行某个操作
peek()  // 与forEach方法类似,但该方法会返回一个新的流,forEach无返回。
limit()  // 截取流中地前几个元素
skip()  // 调高流中前面几个元素
toArray() // 将流转换为数组
reduce() // 归约,将每个元素组合起来形成一个新的值
collect()  // 对流中元素汇总
anyMatch() // 匹配流中地元素,类似方法有 allMatch() noneMatch()
findFirst() // 查找第一个元素
findAny() //类似findfirst()
max()  // 求最大值
min()  // 求最小值
count()  // 求总数

Stream provides a new method 'forEach' to iterate over each item in the stream.
The Collectors class implements many reduction operations, such as converting streams into collections and aggregating elements.

Example usage:
1) Create a Stream.
2) In one or more steps, specify intermediate operations that transform the initial Stream into another Stream.
3) Use a terminating operation to produce a result that forces the deferred operation before it to execute immediately. After this, the Stream will not be used again.

insert image description here

1.3. JavaFX
In 2007, Sun introduced a new technology—JavaFX, making it a competitor of Flash. It runs on the Java VM, but has its own programming language called JavaFX Script. The language is optimized specifically for animation and other effects. In 2011, Oracle released a new version, JavaFX2.0, which provides Java API and no longer needs to be written in another language. In the Java7 update6 version, JavaFX2.2 has been bound to JDK and JRE, and the version number of JavaFX has been consistent with Java since then.

1.4 New Date and Time APIs
Java 8 provides many new APIs under the java.time package.
Two more important APIs:
Local (local) − simplifies the processing of date and time, and there is no problem of time zone.
Zoned − Handles datetimes with the specified time zone.
The new java.time package covers all operations for dealing with dates, times, date/times, time zones, instants, during and clocks.

1.5 Concurrency Enhancement
Aiming at atomic value operations, it provides APIs of LongAdder and LongAccumulator.
ConcurrentHashMap enhancements: lambda expressions are provided for atomic operations, batch operations, etc.
Arrays provides parallel array operations.
CompletableFuture API is provided for multi-threaded operation.

1.6 Nashorn
Java comes with a javascript interpreter Rhino (rhino), which is an open source JavaScript interpreter written in Java. Rhino works pretty well, but the speed is average.

After Java8, a more efficient Javascript interpreter is created with the help of new JVM instructions designed for dynamic languages. And thus the Nashorn project was born. Nashorn is very fast, and it allows you to integrate Java and Javascript on a highly tuned virtual machine. It also has excellent compatibility with the ECMAScript standard for Javascript.

1.7 Other Improvements

String: String adds the join method.
Number classes: Short, Integer, Long, Float, Double provide sum, max, min to be used as aggregation functions in stream operations. The Integer class supports unsigned data calculations. For example, the previous Byte represented the range from -128 to 127. Now you can call the static method Byte.toUnsignedInt(b) to obtain a value from 0 to 255.
 New math functions: addExact, multiplyExact, etc., floorMod.
Collection: Add removeIf: All matching values ​​will be deleted. Added replaceAll method. The Iterator interface adds forEachRemaining: the ability to pass the remaining iteration elements to a function. The Comparator interface adds many useful methods, comparing: Accepts a "key extractor" function that maps a type T to a comparable type (such as String).
Files: Files provides some convenient methods for reading file lines and accessing directory entries using streams. Provides official Base64 encoding/decoding.
Annotations: Provides repeatable annotations and annotations that can be used for types.
Others: The Objects tool class has been added, and java.util.Logger has added lambda support for delaying messages. Regular expressions add named capturing groups. JDBC is upgraded to 4.2 – Date, Time and Timestamp classes in the java.sql package provide some methods that can be converted to and from the corresponding LocalDate, LocalTime and LocalDateTime classes in the java.time package.

Guess you like

Origin blog.csdn.net/weixin_40307206/article/details/107644526