Common function interfaces
-
The use of functional interface
-
Interface MyFunctionalInterface
package cn.xiaoge.day20.demo01; public class Demo { public static void show(MyFunctionalInterface myInter) { myInter.method(); } public static void main(String[] args) { // Call the show method. The parameter of the method is an interface, so the implementation class object of the interface can be passed show(new MyFunctionalInterfaceImpl()); // Call the show method. The parameter of the method is an interface, so we can pass the anonymous inner class of the interface show(new MyFunctionalInterface() { @Override public void method() { System.out.println("Using anonymous inner class to rewrite methods in interface"); } }); // Call show method. The parameter of the method is a functional interface, so we can lambda expression show(()->{ System.out.println("Use lambda Overriding methods in an interface"); }); // Simplify with lambda show(()-> System.out.println("Use lambda Methods in interface rewriting in simplified way")); } }
-
Class MyFunctionalInterfaceImpl
package cn.xiaoge.day20.demo01; /* @Override annotation Check if the method is an override method Yes: compilation succeeded No: compilation failed */ public class MyFunctionalInterfaceImpl implements MyFunctionalInterface{ @Override public void method() { System.out.println("Implementing methods in class rewriting interface"); } }
-
demo
package cn.xiaoge.day20.demo01; public class Demo { public static void show(MyFunctionalInterface myInter) { myInter.method(); } public static void main(String[] args) { // Call the show method. The parameter of the method is an interface, so the implementation class object of the interface can be passed show(new MyFunctionalInterfaceImpl()); // Call the show method. The parameter of the method is an interface, so we can pass the anonymous inner class of the interface show(new MyFunctionalInterface() { @Override public void method() { System.out.println("Using anonymous inner class to rewrite methods in interface"); } }); // Call show method. The parameter of the method is a functional interface, so we can lambda expression show(()->{ System.out.println("Use lambda Overriding methods in an interface"); }); // Simplify with lambda show(()-> System.out.println("Use lambda Methods in interface rewriting in simplified way")); } } // Operation result //Implementing methods in class rewriting interface //Using anonymous inner class to rewrite methods in interface //Using lambda to rewrite the methods in the interface //Rewriting methods in interfaces with lambda simplification
-
-
Performance waste log case & optimizing log case with Lambda
-
Interface MessageBuilder
package cn.xiaoge.day20.demo02; @FunctionalInterface public interface MessageBuilder { // Define an abstract method for splicing messages, and return the spliced messages public abstract String builderMessage(); }
-
Demo01Logger
package cn.xiaoge.day20.demo02; /* Log case A performance waste problem was found in the following code Call the showLog method. The second parameter passed is a concatenated string First, splice the strings, and then call the showLog method showLog Method if the log registration passed is not level 1 The spliced string will not be printed So I feel that the strings are spliced in white, which is wasteful */ public class Demo01Logger { public static void showLog(int level, String msg){ if (level == 1) { System.out.println(msg); } } public static void main(String[] args) { // Define three log information String msg1 = "Hello"; String msg2 = "World"; String msg3 = "Java"; // Call the showLog method to pass the log level and log information showLog(1, msg1+msg2+msg3); } } // Operation result HelloWorldJava
-
Demo02Lambda
package cn.xiaoge.day20.demo02; /* Optimizing log cases with Lambda Lambda Features: delayed loading Lambda Must have a functional interface */ import java.io.File; public class Demo02Lambda { // Define a method of real log, the registration of method parameter delivery log and MessageBuilder interface public static void showLog(int level, MessageBuilder msgBuild){ if (level == 1){ System.out.println(msgBuild.builderMessage()); } } public static void main(String[] args) { // Define three log information String msg1 = "Hello"; String msg2 = "World"; String msg3 = "Java"; // Call the showLog method, the parameter MessageBuilder is a functional interface, so pass the Lambda expression showLog(1, ()->{ // Returns the concatenated string return msg1 + msg2 + msg3; }); /* Use Lambda expressions as parameters, just pass the parameters to the showLog method Only if the conditions are met, the log level is level 1 Will call the method buildmessage in the interface MessageBuilder String splicing If the conditions are not met, the log level is not level 1 Then the method buildmessage in the MessageBuilder interface will not execute So the concatenated string code will not execute either So there's no performance waste */ } } // Operation result HelloWorldJava
-
-
Functional interface as method parameter case
-
Demo01Runnable
package cn.xiaoge.day20.demo03; /* For example, the java.lang.Runnable interface is a functional interface, If you have a startThread method that uses this interface as a parameter, you can use Lambda to pass parameters. In fact, there is no essential difference between this situation and the Thread class's constructor parameter of Runnable. */ public class Demo01Runnable { // Define a method startThread. The parameter of the method is the interface Runnable public static void startThread(Runnable runnable){ new Thread(runnable).start(); } public static void main(String[] args) { // Call the startThread method. The parameter of the method is an interface. Then we can pass the anonymous inner class of the interface startThread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "--->" + "Thread start"); } }); // Instead of startThread method, the parameter of method is a function interface, so Lambda expression can be passed startThread(()->{ System.out.println(Thread.currentThread().getName() + "--->" + "I am Lambda thread"); }); // Optimizing Lambda startThread(()->System.out.println(Thread.currentThread().getName() + "--->" + "I am Lambda thread")); } } // Operation result Thread-0--->Thread start Thread-1--->I am Lambda thread Thread-2--->I am Lambda thread
-
-
Function is the return value of an interface as a method
-
Demo02Comparator
package cn.xiaoge.day20.demo03; /* If the return value type of a method is a functional interface, a Lambda expression can be returned directly. When you need to obtain an object of java.util.Comparator interface type as a sorter through a method, you can call this method to obtain. */ import java.util.Arrays; import java.util.Comparator; public class Demo02Comparator { // Define a method whose return value type uses the function interface Comparator public static Comparator<String> getComparator(){ // The return value type of method is an interface, so we can return the anonymous inner class of this interface /*return new Comparator<String>() { @Override public int compare(String o1, String o2) { // Sort strings in descending order return o2.length() - o1.length(); } };*/ // The return value of the method is a functional interface, so we can use a Lambda expression /*return (String o1, String o2)-> { // Sort strings in descending order return o2.length() - o1.length(); };*/ // Optimize Lambda expressions and define sorting rules return (o1, o2)->o2.length() - o1.length(); } public static void main(String[] args) { // Create an array of strings String[] arr = {"aaa", "b", "cccc", "dddddd"}; // Output array before sorting System.out.println(Arrays.toString(arr)); // Call the sort method in Arrays to sort the strings Arrays.sort(arr, getComparator()); System.out.println(Arrays.toString(arr)); } } // Operation result [aaa, b, cccc, dddddd] [dddddd, cccc, aaa, b]
-
-
Common functional interface ﹣ Supplier interface
-
Demo01Supplier
package cn.xiaoge.day20.demo04; /* Common functional interface java.util.function.Supplier<T> The interface contains only one method without parameters: T get(). Used to get object data of the type specified by a generic parameter. Supplier(T)An interface is called a production interface. If you specify the generic type of the interface, the get method in the interface will produce data of what type */ import java.util.function.Supplier; public class Demo01Supplier { // Define a method. The parameter of the method passes the supplier < T > interface. If the generic type executes String, the get method will return a String public static String getString(Supplier<String> s){ return s.get(); } public static void main(String[] args) { // Call the getString method. The parameter Supplier of the method is a functional interface, so you can pass the Lambda expression System.out.println(getString(()-> { // Produce a string and return return "hello world"; })); // Optimizing lambda expressions System.out.println(getString(()-> "hello world")); } } // Operation result hello world hello world
-
Demo02Test
package cn.xiaoge.day20.demo04; /* Exercise: finding the maximum value of array elements The Supplier interface is used as the parameter type of the method, and the maximum value in the int array is calculated by lambda expression Hint: for the generics of interface, please use java.lang.Integer class */ import java.util.function.Supplier; public class Demo02Test { // Define a method to get the maximum value of the elements in the array of type int, pass the Supplier interface for the parameters of the method, and use Integer for generics public static int getMax(Supplier<Integer> sup) { return sup.get(); } public static void main(String[] args) { // Define an array of type int and assign int[] arr = {2, 4, 1, 6, 3, 7, 5}; // Call the getMax method. The parameter Supplier of the method is a functional interface, so you can pass the Lambda expression int maxValue = getMax(()->{ // Gets the maximum value of the array and returns // Define a variable, assign the first element in the array to the variable, and record the maximum value of the element in the array int max = arr[0]; // Maximum value // Traverse the array to get the maximum value in the array for(int i=1; i < arr.length; i++){ // Compare with other elements and maximum values if (arr[i] > max) { // If arr[i] is greater than max, replace Max as the maximum max = arr[i]; } } // Return maximum return max; }); System.out.println("The maximum value of an element in an array: " + maxValue); } } // Operation result //The maximum value of an element in an array: 7
-
-
Common functional interface ﹣ Consumer
-
Demo01Consumer
package cn.xiaoge.day20.demo05; /* java.util.function.Consumer<T>The interface is just the opposite of the Supplier interface It does not produce a data, but consumes a data whose data type is determined by generics Consumer The interface contains the abstract method void accept (t t t), which means to consume data of a specified generic type Consumer The interface is a consumer interface. Its generics specify what type of data can be consumed using the accept method As for how to consume (use), you need to customize (output, calculation...) */ import java.util.Arrays; import java.util.function.Consumer; public class Demo01Consumer { /* Define a method Method to pass the name of a string The parameter of the method passes the Consumer interface, and the generic uses String You can use the Consumer interface to consume string names */ public static void method(String name, Consumer<String> con) { con.accept(name); } public static void main(String[] args) { // Call the method method method and pass the string name. The other parameter of the method is the Consumer interface, which is a functional interface, so you can pass the Lambda expression method("Kim Hyun A", (String name)->{ // Consume the passed string // Consumption mode, output string directly System.out.println(name); // Consumption mode, reverse the string output // StringBuffer(name) the content of the string buffer is the string value // reverse reverses the array value and returns the character array // Rewriting toString method to convert character array to string String reName = new StringBuffer(name).reverse().toString(); System.out.println(reName); }); } } // Operation result //Kim Hyun A //Ya Jin Jin
-
Demo02AndThen
package cn.xiaoge.day20.demo05; /* Consumer The default method of interface andThen Function: two Consumer interfaces are required, which can be combined to consume data Such as: Consumer<String> con1 Consumer<String> con2 String s = "hello"; con1.accept(s); con2.accept(s); Equate to Linking two Consumer interfaces for consumption con1.andThen(con2).accept(s); Who writes first and who consumes first */ import java.util.function.Consumer; public class Demo02AndThen { // Define a method. The parameters of the method pass a string and two Consumer interfaces. The generics of the Consumer interface use strings public static void method(String s, Consumer<String> con1, Consumer<String> con2) { // Using the andThen method, link the two Consumer interfaces together to consume data con1.andThen(con2).accept(s); // con1 links con2. First execute con1 consumption data, and then execute con2 consumption data } public static void main(String[] args) { method("Hello", (String s1)->{ // Convert string to uppercase output System.out.println(s1.toUpperCase()); }, (String s2)->{ // Convert string to lowercase output System.out.println(s2.toLowerCase()); }); } } // Operation result HELLO hello
-
Demo03Test
package cn.xiaoge.day20.demo05; /* Practice: There are multiple pieces of information in the string array. Please follow the format "Name: XX". Gender: XX. " To print out the information. The action to print the name is required as the first Lambda instance of the Consumer interface, Take the action of printing gender as the Lambda instance of the second Consumer interface, "Splices" two Consumer interfaces in sequence */ import java.util.function.Consumer; public class Demo03Test { // Define a method to pass an array of String type and two Consumer interfaces. Generic type uses String public static void method(String[] arr, Consumer<String> con1, Consumer<String> con2){ for (String s : arr) { // Use the andThen method to link two Consumer interfaces and consume strings con1.andThen(con2).accept(s); } } public static void main(String[] args) { String[] arr = {"Kim Hyun A,female", "Kim Su Hyun,female", "G-Dragon,male"}; method(arr, (String s1)->{ String name = s1.split(",")[0]; System.out.print("Full name: " + name); }, (String s2)->{ String gender = s2.split(",")[1]; System.out.println(".Age: " + gender + ". "); }); } } // Operation result //Full name: Kim Hyun A.Age: female. //Full name: Kim Su Hyun.Age: female. //Full name: G-Dragon.Age: male.
-
-
Common functional interface
-
Demo01Predicate
package cn.xiaoge.day20.demo06; /* java.util.function.Predicate<T>Interface Function: judge the data of a data type and return a boolean value Predicate The interface contains an abstract method: boolean test(T t): Method used to judge data of specified data type Result: Meet the conditions, return true If the condition is not met, return false */ import java.util.function.Predicate; public class Demo01Predicate { /* Define a method Parameter to pass a String of type String Pass a Predicate interface, generic uses String Use the method test in Predicate to judge the string and return the result */ public static boolean checkString(String s, Predicate<String> p) { return p.test(s); } public static void main(String[] args) { // Define a string String s = "abcdef"; // Call the checkString method to verify the string, and pass the string and Lambda expression /*boolean isTrue = checkString(s, (String str)->{ return s.length() > 5; });*/ // Optimizing Lambda boolean isTrue = checkString(s, (str)->s.length() > 5); System.out.println(isTrue); } } // Operation result true
-
Demo02Predicate_and
package cn.xiaoge.day20.demo06; /* Logical expressions: conditions that can link multiple judgments &&: And operator, false if false ||: Or operator, true if true !: Non (negate) operator, false if not true, true if not false Requirement: judge a string with two conditions 1. Determine whether the length of the string is greater than 5 2. Determine whether the string contains a If the two conditions must be met at the same time, we can use the & & operator to link the two conditions Predicate There is a method and in the interface, representing and relating, which can also be used to connect two judgment conditions default Predicate<T> and(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> this.test(t) && other.test(t); } The two inner judgment conditions of the method are also linked by the & & operator */ import java.util.function.Predicate; public class Demo02Predicate_and { /* Define a method, parameters of the method, pass a string Pass two Predicate interfaces One is used to determine whether the length of the string is greater than 5 A is used to determine whether the string contains a Both conditions must be met at the same time */ public static boolean method(String s, Predicate<String> p1, Predicate<String> p2) { // return p1.test(s) && p2.test(s); return p1.and(p2).test(s); // Equivalent to return p1.test (s) & & p2.test (s); } public static void main(String[] args) { String s = "abcdef"; boolean b = method(s, (String s1)->{ return s1.length() > 5; }, (String s2)->{ // Contains determines whether the string contains a return s2.contains("a"); }); System.out.println(b); } } // Operation result true
-
Demo03Predicate_or
package cn.xiaoge.day20.demo06; /* Logical expressions: conditions that can link multiple judgments &&: And operator, false if false ||: Or operator, true if true !: Non (negate) operator, false if not true, true if not false Requirement: judge a string with two conditions 1. Determine whether the length of the string is greater than 5 2. Determine whether the string contains a If one condition is satisfied, we can use the 𞓜 operator to link two conditions Predicate There is a method or, representation or relationship in the interface, which can also be used to connect two judgment conditions default Predicate<T> or(Predicate<? super T> other) { Objects.requireNonNull(other); return (t) -> this.test(t) || other.test(t); } The two inner judgment conditions of the method are also linked using the | | - operator */ import java.util.function.Predicate; public class Demo03Predicate_or { /* Define a method, parameters of the method, pass a string Pass two Predicate interfaces One is used to determine whether the length of the string is greater than 5 A is used to determine whether the string contains a One condition is satisfied */ public static boolean method(String s, Predicate<String> p1, Predicate<String> p2) { // return p1.test(s) || p2.test(s); return p1.or(p2).test(s); // Equivalent to return p1.test (s) | p2.test (s); } public static void main(String[] args) { String s = "abcde"; boolean b = method(s, (String s1)->{ return s1.length() > 5; }, (String s2)->{ // Contains determines whether the string contains a return s2.contains("a"); }); System.out.println(b); } } // Operation result true
-
Demo04Predicate_negate
package cn.xiaoge.day20.demo06; /* Requirement: judge whether the length of a string is greater than 5 Returns false if the length of the string is greater than 5 Returns true if the length of the string is not greater than 5 So we can use the reverse sign to reverse the result of judgment Predicate There is a method "negate" in the interface, which also means "negate" default Predicate<T> negate() { return (t) -> !test(t); } */ import java.util.function.Predicate; public class Demo04Predicate_negate { /* Define a method, parameters of the method, pass a string Use the Predicate interface to determine if the length of a string is greater than 5 */ public static boolean method(String s, Predicate<String> p) { return p.negate().test(s); } public static void main(String[] args) { boolean b = method("123456", (String s)->{ // Determine whether the length of the string is greater than 5, and return the result return s.length()>5; }); System.out.println(b); } } // Operation result false
-
Demo05Test
package cn.xiaoge.day20.demo06; /* Exercise: set information filtering The information of "name + gender" in the array is as follows, String[] array = { "Delireba, female "," gulinaza, female "," marzaha, male "," Zhao Liying, female "}; Please filter the qualified strings into the collection ArrayList through the assembly of the Predicate interface, Two conditions need to be met at the same time: 1. Must be female; 2. The name is 4 words. Analysis: 1. There are two judgment conditions, so you need to use two Predicate interfaces to judge the conditions 2. Two conditions must be met at the same time, so use and method to link two judgments */ import java.util.ArrayList; import java.util.function.Predicate; public class Demo05Test { /* Define a method Method to pass an array of personnel information Pass two predict interfaces to filter the information in the array Store the information that meets the conditions in the ArrayList collection and return */ public static ArrayList<String> filter(String[] arr, Predicate<String> p1, Predicate<String> p2) { // Define an ArrayList collection to store filtered information ArrayList<String> list = new ArrayList<>(); // Traverse the array to get every piece of information in the array for (String s : arr) { // Use the method test in the Predicate interface to get more strings for judgment boolean b = p1.and(p2).test(s); if(b){ // If the conditions are met, store the information in the ArrayList set list.add(s); } } // Return the set return list; } public static void main(String[] args) { // Define an array to store strings String[] arr = {"Di Ali Gerba,female", "Guli Nazha,female", "Mar Zaha,male", "Zhao Liying,female"}; // Call the filter method, passing the string array and two Lambda expressions ArrayList<String> list = (filter(arr, (String s1)->{ String name = s1.split(",")[0]; // Determine if the name length is equal to 4 return name.length() == 4; }, (String s2)->{ String gender = s2.split(",")[1]; // Judge whether the gender is female return "female".equals(gender); })); System.out.println(list); } } // Operation result [Di Ali Gerba,female, Guli Nazha,female]
-
-
Common Function interface
-
Demo01Function
package cn.xiaoge.day20.demo07; /* java.util.function.Function<T,R> Interface is used to get data of one type from data of another, The former is called pre condition, and the latter is called post condition. Function The main abstract method in the interface is: R apply (t t t). The result of type R is obtained according to the parameter of type T. For example, convert String type to Integer type. */ import java.util.function.Function; public class Demo01Function { /* Top one Method to pass an integer of type string The parameter list of the method passes a Function interface, and the generic type uses < string, integer > Use the method apply in the Function interface to convert an Integer of string type to an Integer of Integer type */ public static void change(String s, Function<String, Integer> f) { // Integer i = f.apply(s); int i = f.apply(s); // Automatic dismantling System.out.println(i); } public static void main(String[] args) { // Call the change method, pass the integer of string type, and Lambda expression change("123456", (String s)->{ // Converts an Integer of type string to an Integer of type Integer return Integer.parseInt(s); }); // Optimize Lambda expressions change("123456", (String s)->Integer.parseInt(s)); } } // Operation result 123456 123456
-
Demo02Function_andThen
package cn.xiaoge.day20.demo07; /* Function The default method andThen: in the interface is used for composite operations Demand: Convert "123" of String type to Integer type, and add 10 to the converted result Convert the data of Integer type after adding to String type Analysis: Changed twice The first time is to convert String type to Integer type So we can use function < string, integer > F1 Integer num = f1.apply("123") + 10; The second time is to convert Integer type to String type So we can use function < integer, string > F2 String s = f2.apply(num); We can use the andThen method to combine the two transformations String s = f1.andThen(f2).apply("123"); f1 Call the apply method first to convert the string to Integer f2 When calling the apply method, convert the Integer to a string */ import java.util.function.Function; public class Demo02Function_andThen { public static void change(String s, Function<String, Integer> f1, Function<Integer, String> f2) { System.out.println(f1.andThen(f2).apply(s)); } public static void main(String[] args) { change("123", (String s)->{ // Convert string to Integer type int num = Integer.parseInt(s); // Automatic dismantling return num+10; }, (Integer num)->{ // Convert Integer type to string or directly + "" to string String s = Integer.toString(num); // getClass get variable type System.out.println(s.getClass()); return s; }); // Optimize Lambda expressions change("123", (s)->Integer.parseInt(s)+10, (num)->Integer.toString(num)); } } // Operation result class java.lang.String 133 133
-
Demo03Test
package cn.xiaoge.day20.demo07; /* Exercise: splicing custom function models subject Please use Function to splice the Function models. The multiple Function operations that need to be performed in order are: String str = "Zhao Liying, 20 "; Analysis: 1. Intercept the age part of the string to get the string; Function<String, String> "Zhao Liying, 20 "-- > 20“ 2. Convert the string of the previous step to a number of type int; Function<String, Integer> "20" --> 20 3. Add up the int number of the previous step by 100 to get the result int number. Function<Integer, Integer> 20 --> 120 */ import java.util.function.Function; public class Demo03Test { /* Define a method Parameter passes a string containing the name and age Parameters are passed 3 Function interfaces for type conversion */ public static int change(String s, Function<String, String> f1, Function<String, Integer> f2, Function<Integer, Integer> f3) { // Use the andThen method to combine the three transformations return f1.andThen(f2).andThen(f3).apply(s); } public static void main(String[] args) { // Call change method, pass string and 3 Lambda expressions int num = change("Zhao Liying,20", (String s1)->{ // "Zhao Liying, 20" -- > 20 " return s1.split(",")[1]; }, (String s2)->{ // "20" --> 20 return Integer.parseInt(s2); }, (Integer i)->{ // 20 --> 120 return i+100; }); System.out.println(num); // Optimize Lambda expressions int num2 = change("Zhao Liying,20", (s1)->s1.split(",")[1], (s2)->Integer.parseInt(s2), (i)->i+100); System.out.println(num2); } } // Operation result 120 120
-