Interface functions described
are primarily java.util.function package. Here are the most common of several interfaces.
1. Supplier Interface
@FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); }
2. Consumer Interface
@FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t); }
3. Function Interface
@FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. * * @param t the function argument * @return the function result */ R apply(T t); }
4. Predicate Interface
@FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t); }
Supplier interfaces
java.util.function.Supplier <T> interfaces, it means "supply", corresponding to Lambda expressions need to "provide external" line with a generic type of object data.
/ ** * @author the WGR * @Create 2020/3/24 - 21:46 * / public class Demo02Supplier { // using a Lambda expression returns the maximum array elements public static void main (String [] args) { the System. Out.println ( "started" ); the printMax (() -> { int [] = {ARR. 11, 99, 88, 77, 22 is }; Arrays.sort (ARR); // Sort ascending return ARR [ARR. length -. 1 ]; }); } public static void the printMax (Supplier <Integer> supplier) { System.out.println("aa"); int max = supplier.get(); System.out.println("max = " + max); } }
Consumer Interface
java.util.function.Consumer <T> Interface by contrast, it is not a production data, a consumption data but that the data type is determined by the generic parameter.
public class Demo03Consumer { public static void main(String[] args) { printHello( str ->{ //HELLO JDK8 System.out.println(str.toUpperCase()); }); } public static void printHello(Consumer<String> consumer) { consumer.accept("hello jdk8"); } }
public class Demo04ConsumerAndThen { public static void main(String[] args) { printHello( str1 ->{ System.out.println(str1.toUpperCase()); },str2 ->{ System.out.println(str2.toLowerCase()); }); } public static void printHello(Consumer<String> c1, Consumer<String> c2) { String str = "Hello Jdk8"; // default Consumer<T> andThen(Consumer<? super T> after) { // Objects.requireNonNull(after); // return (T t) -> { accept(t); after.accept(t); }; // } c2.andThen(c1).accept(str); } }
Since c2 at the front, so the operation to go to the operation c2 c1, the results shown in FIG.
Function Interface
java.util.function.Function <T, R> interface is used to obtain a data type to another data type according to the former is called pre-conditions, which is referred to as post-conditions. There are parameters return value.
public class Demo05Function { public static void main(String[] args) { getNumber( str ->{ return Integer.parseInt(str); }); } public static void getNumber(Function<String, Integer> function) { String num = "20"; Integer apply = function.apply(num); System.out.println(apply); } }
public class Demo06FunctionAndThen { public static void main(String[] args) { getNumber(str -> { return Integer.parseInt(str); }, num -> { return num * 5; }); } public static void getNumber(Function<String, Integer> f1, Function<Integer, Integer> f2) { String num = "20"; Integer integer = f1.andThen(f2).apply(num); System.out.println(integer); //100 } }
Predicate interfaces
Sometimes we need to be determined for certain types of data, to thereby obtain a boolean result. Then you can use java.util.function.Predicate <T> interface.
public class Demo07Predicate { public static void main(String[] args) { isLongName( str -> { return str.length() > 3; }); } public static void isLongName(Predicate<String> predicate) { boolean test = predicate.test("新年快乐"); System.out.println(test); } }
public class Demo08Predicate_And_Or_Negate { // using a Lambda expression is determined i.e. a string containing W, also comprising H // using a Lambda expression determines a string comprising or containing W H // using a Lambda expression determined not a string comprising W is public static void main (String [] args) { Test ((String STR) -> { // determines whether or not containing W is return str.contains ( "W is" ); }, (String STR) -> { // Analyzing contain H return str.contains ( "H" ); }); } public static voidTest (the Predicate <String> P1, the Predicate <String> P2) { String STR = "the Hello World" ; Boolean B = p1.and (P2) .test (STR); IF (B) { System.out.println ( " i.e. comprising W, also comprising H " ); } // using a Lambda expression determines a string comprising or containing W H Boolean B1 = p1.or (P2) .test (STR); IF (B1) { the System.out .println ( "W comprises or comprise H" ); } // using a Lambda expression determines a string contains W Boolean B2 = p1.negate () Test ( "W the Hello." ); // o negate take equivalent anti-!boolean if (b2) { System.out.println("不包含W"); } } }