Java8? Can you give an example of practical application? thank you
Java8? Can you give an example of practical application? thank you
is mainly used in functional programming, and can also be used in any other suitable place, in order to pass the function (lambda) as a variable.
as for examples, there are a lot of them on the Internet.
take an example from java8.
java8 introduces an overloaded version of the log method, which takes a Supplier as a parameter. The function signature of this alternative version of the log method is as follows:
public void log(Level level, Supplier<String> msgSupplier)
you can call it in the following ways:
logger.log(Level.FINER, () -> "Problem: " + generateDiagnostic());
if the level of the logger is set properly, the log method executes the Lambda expression passed in as a parameter internally. The internal implementation of the Log method described in this
is as follows:
public void log(Level level, Supplier<String> msgSupplier){
if(logger.isLoggable(level)){
log(level, msgSupplier.get());
}
}
if you find that you need to frequently query the status of an object from the client code (such as the status of the logger in the previous example), just to pass parameters and call a method of the object (such as outputting a log), then consider implementing a new method that takes Lambda or method expression as a parameter, and the new method calls the original method after checking the state of the object. As a result, your code will become easier to read (clearer in structure) and better encapsulated (the state of the object will not be exposed to the client code).
description trouble boss to point out how to reconstruct the operations of different objects with the same method? At the bottom of is the code. The save methods here are all the same operation. The object is used as a reference here. I just want...
github downloaded nginx-admin,java8 compiled, imported eclipse Times error, looked at the following code, there are several similar statements import com.jslsolucoes.nginx.admin.model.Nginx; import com.jslsolucoes.nginx.admin.model.Nginx_; In the mode...
use Java8 time-related API to get the Monday of a week, such as 2018-W48, which indicates the 48 weeks of 2018. How to get the time of 48 weeks of Monday? ...
problem description java.util.function,, A code that determines whether a field is empty has been rewritten (some business definitions assume that a field is empty by default if it is equal to-1). ,,,: related codes Old Code public static &...