First, the composition of the lambda expression is mainly passed in the parameter list (parameter 1, parameter 2, ...). ), Arrow->, and return value, can be a value, or struct.
As a small example:
When you are not using a lambda expression:
Import java.util.stream.stream;/** * * @author Kangjun */public class Demo {public static void main (string[] args { Dog dog1 = new Dog ("Dog1", 2); Dog dog2 = new Dog ("Dog2", 1); Dog Dog3 = new Dog ("Dog3", 3); Dog[] Dogs = {DOG1,DOG2,DOG3}; Printdogs (dogs); Arrays.sort (Dogs, new comparator<dog> () { @Override public int compare (dog T, dog T1) { return T.getweight ()-t1.getweight (); } ); Printdogs (dogs); } public static void Printdogs (dog[] dogs) {for (Dog d:dogs) { System.out.println (d); } System.out.println (); }}
A lambda expression is added and can be written as follows (implemented in 3 ways):
Import java.util.stream.stream;/** * * @author Kangjun */public class Demo {public static void main (string[] args) { Dog dog1 = new Dog ("Dog1", 2); Dog dog2 = new Dog ("Dog2", 1); Dog Dog3 = new Dog ("Dog3", 3); Dog[] Dogs = {DOG1,DOG2,DOG3}; Printdogs (dogs); Arrays.sort (dogs, (M,n)->m.getweight ()-n.getweight ()); Lambda method One//arrays.sort (dogs, (Dog m,dog N)->integer.compare (M.getweight (), N.getweight ())),//lambda method two Printdogs (dogs); Method three stream<dog> Dogstream = Stream.of (dogs); stream<dog> Sorteddogstream = dogstream.sorted ((dog m, Dog N) integer.compare (M.getweight (), N.getweight ()) ); Sorteddogstream.foreach (D-System.out.print (d)); } public static void Printdogs (dog[] dogs) {for (Dog d:dogs) {System.out.println (d); } System.out.println (); }}
After reading it is not very excited.
Here are a few more examples of how the parameter is implemented:
Import java.util.arrays;/** * * @author Kangjun */public class Standardsyntaxlambda { private static final string[] Ar r = {"program", "Creek", "is", "a", "Java", "Site"}; public static void Main (string[] args) { arrays.sort (arr, (String a,string b), Integer.compare (A.length (), B.length ())); System.out.println (arrays.tostring (arr));} }
Import java.util.arrays;/** * * @author Kangjun */public class Parametertypeinferredlambda { private static final Stri Ng[] arr = {"program", "Creek", "is", "a", "Java", "Site"}; public static void Main (string[] args) { arrays.sort (arr, (M,n), Integer.compare (M.length (), N.length ())); System.out.println (arrays.tostring (arr));} }
Import java.util.arrays;/** * * @author Kangjun */public class Multiplelinescodelambda { private static final string[] arr = {"program", "Creek", "is", "a", "Java", "Site"}; public static void Main (string[] args) { arrays.sort (arr, (String m,string N) { return m.length ()-N.length ( ); }); System.out.println (arrays.tostring (arr)); } }
Import java.util.stream.stream;/** * * @author Kangjun */public class Singleparameterinferredtypelambda { private Static final string[] arr = {"program", "Creek", "is", "a", "Java", "Site"}; public static void Main (string[] args) { stream<string> arrstream = Stream.of (arr); stream<string> Sortedstream = arrstream.sorted ((A, B) a.length ()-b.length ()); Sortedstream.foreach (X-System.out.println (x));} }
Several forms of the Java LAMBDA expression