What are the main applications of Supplier,Predicate introduced in Java8?

What is the main application of Supplier,Predicate introduced in

Java8? Can you give an example of practical application? thank you

Mar.17,2021

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).

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b39548-2c17a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b39548-2c17a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?