There is no way to use lambda expressions:
New Comparator<transaction>() { @Override publicint compare (Transaction O1, Transaction O2) { return o1.getvalue (). CompareTo (O2.getvalue ()); } };
How lambda expressions are written:
Comparator<transaction> byyear = (O1, O2), O1.getvalue (). CompareTo (O2.getvalue ());
Three parts of a lambda expression:
parameter list the parameters of the Compare method in comparator are used here, two transaction
Arrow arrows separate the argument list from the lambda body.
The lambda body compares two transaction years, and the expression is the return value of the lambda
Some examples of lambda and use cases are provided below
(list<string> List),new, { System.out.println ("year =" + s.length ();(intint b), A * b;
Where to use the lambda interface
You can use a lambda expression on a functional interface,
A functional interface is simply an interface that defines an abstract method, and the inheritance does not work: it is a concrete implementation example of a functional interface.
Example:
New Runnable () {// use anonymous class @Override public void Run () { System.out.println ("Hello World 2") ); } }; Process (r1); // print Hello World 1 Process (R2); // print Hello World 2 Process ((), System.out.println ("Hello World 3"); // using a direct pass-through lambda expression as an implementation instance print Hello World 3
If we look at the new Java Api, we find that the functional interface has a @functionalinterface callout, which indicates that the interface is designed as a functional interface, and if you use that annotation and it is not a functional interface, the compiler returns a hint error.
/** * The <code>Runnable</code> interface should is implemented by any * class whose instances is intended to be Executed by a thread. The * class must define a method of no arguments called <code>run</code>. * <p> * This interface are designed to provide a common protocol for objects. * Wish to execute code while they a Re active. For example, * <code>Runnable</code> are implemented by class <code>thread</code>. * Being Active simply means that a thread have been started and have not * yet been stopped. * <p> * In addition, <code>Runnable</code> provides the means for a class to being * active while not SUBCL Assing <code>thread</code>. A class that implements * <code>Runnable</code> can run without subclassing <code>Thread</code> * By instantiating a <code>Thread</code> instance and passing itself in * as the target. In more cases, the <code>Runnable</code> inTerface should * is used if you is only planning to override the <code>run () </code> * method and no other < ;code>thread</code> methods. * This was important because classes should not being subclassed * unless the programmer intends on modifying or enhancing the Fundamental * behavior of the class. * *@authorArthur van Hoff *@seeJava.lang.Thread *@seejava.util.concurrent.Callable *@sinceJDK1.0*/@FunctionalInterfacepublicInterfaceRunnable {/*** When an object implementing interface <code>Runnable</code> was used * To create a thread, start ing the thread causes the object ' s * <code>run</code> method to being called in that separately executing * Thread. * <p> * The general contract of the method <code>run</code> are that it could take any action wha Tsoever. * * @seeJava.lang.thread#run ()*/ Public Abstract voidrun ();}
Method reference
Method references allow you to reuse existing method definitions and pass them like a lambda. Example of the following sort:
New Arraylist<>(); Inventory.sort (comparator.comparing (apple::getweight)// Sort by weight . Reversed ()// Descending reverse . thencomparing (Apple::getcountry)); // Sort by country if the weight is the same
When you need to use a method reference, the target reference is placed before the delimiter:: Before the method name, for example Apple::getweight refers to the getweight defined in the Apple class, keep in mind that no parentheses are required because you do not actually call this method.
There are three main types of method references:
A method reference to a static method (for example, the parseint method of an integer writes an integer::p arseint).
A method reference to an instance method of any type (for example, the length method of string, writing string::length)
A method reference to an instance method of an existing object (assuming you have a local variable expensive to hold an object of type transaction, he supports the instance method GetValue, then you can write Expensive::getvalue).
Constructor reference
For an existing constructor, you can use its name and keyword new to create one of his references: Classname::new, which functions like a reference type that points to a static method.
Java8 New Features Learning notes (i) Lambda expressions