Summary of common new features in Java8
1.1. Overview of Common Java8 features
1.2. Lambda expressions
A new operator “->” has been introduced in Java8. This operator is called the arrow operator or Lambda operator, left: argument list of Lambda representation, right: The functionality to be performed in Lambda expressions, taking functions as arguments to a method, is often used in shorthand for anonymous inner classes, and Lambda expressions are also the basis for better use of StreamAPI.
1.3. Functional interfaces
An interface that contains only one abstract method is called a functional interface. You can check for a FunctionalInterface using the @functionalinterface annotation. For example, we can define a function interface that handles a number. The number is the parameter of the function interface. Each processing is the implementation of the function interface. A simple example is as follows:
@FunctionalInterface
public interface MyFun {
public Integer getValue(Integer num);
}
Copy the code
public Integer operation(Integer num,MyFun mf){
return mf.getValue(num);
}
Copy the code
@Test
public void test5(a){
Integer num = operation(100,(x)-> x*x);
System.out.println(num);
}
Copy the code
Four core function interfaces
Consumer
Consumer interface: Parameter type T return typevoid
Apply an operation to an object of type T,Consumer
Consumption is passing in a parameter and processing itSupplier
Parameter Type None Return type T Returns an object of type T.Supplier
Supply type is to get some resultsFunction<T,R>
Function interface: parameter type T returns type R applies an operation to an object of type T and returns the result.Function
A function takes an argument, processes it, and returns a resultPredicate
Assertion interface: parameter type T return typeboolean
Determines whether an object of type T satisfies a constraint and returns a Boolean value.Predicate
The predicate type is making some judgments
1.4. Method references and array references
A method reference can be used when an operation to be passed to the Lambda body already has an implementation method. Method reference: Separates the method name from the object or class name with the :: operator, as in:
- Object :: instance method
- Class :: static methods
- Class :: instance methods
The essence of method references is an easier way to replace Lambda expressions
1.5. StreamAPI
Stream is a key abstraction for dealing with collections in Java8. It specifies that you want to test a collection. It can perform very complex operations of finding, filtering, and mapping data. Manipulating collection data using the Stream API is similar to database query queries executed using SQL, and the Stream API provides an efficient and easy-to-use way to process data. A Stream is a data channel used to manipulate sequences of elements generated by data sources (collections, arrays, etc.). Collections are data, streams are computation.
- The Stream itself does not store elements
- Stream does not change the source object; instead, they return a new Stream holding the result
- Stream operations are deferred, which means they wait until the result is needed.
Stream usage
- Create Stream: A data source (collection, array) gets a Stream
- Intermediate operations: A chain of intermediate operations that process data from a data source
- Termination operation: A termination operation that performs an intermediate chain of operations and produces a result.
1.6. LocalDateTime
LocalDateTime is an immutable datetime object that represents a datetime and is usually treated as year-month-day-hour-minute-second. This is also one of the most used in development, such as statistical data based on time, after statistics for the current quarter or the next quarter, or access to current, and then get on the first day of the current month, for example, the last day of the current month, the last day of the next two months, and so on, using LocalDateTime can be very simple, for example, Age based on date of birth:
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDate csrq = LocalDate.parse(csrq, dtf);
LocalDate now = LocalDate.now();
return (int)ChronoUnit.YEARS.between(csrq, now);
Copy the code
1.7. Optional
With the use of microservices, calling other system interfaces, you do not fully know what others will return to you. Before, we could use three operations to null or if null. Now you can eliminate null Pointers more elegantly with Optional.
1.8.Base64
Although this feature is not used as much as previous new features, it has been used in recent project development, just for the record,
String text = "show me the code";
String encoded = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
String decoded = new String(Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
Copy the code
1.9. More detailed information
- Study notes for new Java8 features
- Java8 common time operations