///***JAVA8 function reference a simplified function expression that uses the simple inference of an existing function (omitting the parameter,-> symbol, only the more simplified function expression body), [thus generating the corresponding interface implementation class default abstract method body]. ***
function expressions can be inferred as anonymous classes in Java, so existing functions can be inferred as function expressions. As long as the existing function conforms to the signature of the method to be generated (enter parameter, return value, function name does not matter)
Parameters and return values are obtained from the context of the compiler. Thus eliminating the parameters. You can only provide a method name.
Therefore, the {} function expression, (ARG), can be simplified to take advantage of existing function references: <class or instance name>::<methodname>.
Package Com.github.jdk8.ebook.java8_recipes2nd_edition;import Java.util.arrays;import Java.util.Comparator;import Java.util.list;import Java.util.function.biconsumer;import Java.util.function.bifunction;import Java.util.function.bipredicate;import Java.util.function.binaryoperator;import Java.util.function.booleansupplier;import Java.util.function.consumer;import Java.util.function.Function;import Java.util.stream.collectors;import java.util.stream.stream;/** * * @author doctor * * @time February 4, 2015 morning 8:46:47 */public Class Chapter6code {public static void main (string[] args) {Hellotype hellotype = (ARG) (System.out.println ("Hello" + arg); Hellotype.hello ("Doctor"); Function<string, string> function = (value), "Hello" + value; System.out.println (Function.apply ("Doctor")); biconsumer<string, string> Biconsumer = (A, b) System.out.println (String.Join (",", A, b)); biconsumer.accept ("Biconsumer Hello", "Doctor"); Bifunction<string, String, string> bifunction= (A, b)-String.Join (",", A, b); System.out.println (bifunction.apply ("Bifunction", "Hello"));//Binaryoperator is a special kind of bifunction, the parameter is the same type (enter the parameter, return type) binaryoperator<string> Binaryoperator = (A, b), String.Join (",", A, b); System.out.println (binaryoperator.apply ("Binaryoperator", "Hello")); bipredicate<string, string> bipredicate = (A, b), a.equals (b); System.out.println ("bipredicate:" + bipredicate.test ("AB", "CD")); Booleansupplier Booleansupplier = () true; System.out.println ("Booleansupplier:" + Booleansupplier.getasboolean ()); Consumer<string> Consumer = (a), System.out.println (a), Consumer.accept ("Consumer:" + "Hello"); List<string> collect = Stream.of ("C", "DCA", "cddd", "Abccss", "Ba", "AaB"). Parallel (). Sorted ( Comparator.comparing (string::length)). Collect (Collectors.tolist ()); Collect.foreach (System.out::p rintln);//* * * Java8 function Reference-a simplified function expression that uses the simple inference of an existing function (omitting the parameter,-> symbol, only the more simplified function expression body), [thus generating the corresponding interface implementation class default abstract method body]. function expressions can be inferred as anonymous classes in Java, then the existing lettersThe number can be inferred as a function expression. As long as the existing function conforms to the signature of the method to be generated (entry, return value, function name does not matter)//parameters and return values are obtained from the context of the compiler. Thus eliminating the parameters. You can only provide a method name. Therefore, the {} function expression, (ARG), can be simplified to take advantage of existing function references: <class or instance name>::<methodname>. A method reference is a//simplified form of a lambda expression, which specifies the class name or instance name, Foll Owed by the method to//is called in the following format://<class or instance name>::<methodname>//the Doub Le colon (::) operator specifies a method reference. Since A method reference is a simplified lambda//method, it must implement a functional interface, and the abstract Metho D within the interface must has the same//argument list and return type as the method being referenced. Any arguments is subsequently derived from the//context of the method reference. For instance, consider the same scenario as the solution, whereby-wanted to sort//a array of Player objects by calli ng upon the Player.comparebygoal () method to perform goal comparisons.//the following code Could be written to enable this functionality via a lambda expression:integer[] integers = {12, 45, 1, 66, 33, 22}; System.out.println ("lambda expression before sort:"); Arrays.stream (integers). ForEach (System.out::p rintln); Arrays.sort (integers, (a, B), Integer.compare (A, b)); Generate anonymous class Comparator with lambda expression, implement compare (T O1, T O2) method System.out.println ("After lambda expression is sorted:"); Arrays.stream (integers). ForEach (System.out::p rintln); integer[] Integers2 = {12, 45, 1, 66, 33, 22}; System.out.println ("Before the function reference is sorted:"); Arrays.stream (integers2). ForEach (System.out::p rintln); Arrays.sort (Integers2, Integer::compare); Generate an anonymous class with a function reference, SYSTEM.OUT.PRINTLN ("After the function reference is sorted:"); Arrays.stream (integers2). ForEach (System.out::p rintln);} @FunctionalInterfacepublic interface Hellotype {void Hello (String message);}}
Java8 function Reference--a simplified function expression using simple inference of existing functions as function expressions