[Java] using Lambda ordered set

Here is a simple example of Java lambda expression:

// 1. no arguments, returns a value of 5   
() -> 5   // 2 receives a parameter (type number), returns the value of 2 times   
X -> 2 * X   @ 3. accepts two parameters ( figures), and return to their difference   
(X, Y) -> X - Y   // 4. receiving two int type integer, and return to their   
( int X, int Y) -> X + Y   // . 5. receiving a string object, and printed on the console, nothing is returned (return looks like void)   
(String S) -> of System.out.print (S)
  

  

  

  

In Java, Comparator ordered set of classes are used. In the following example, we will be based on the player's name, surname, name and length of the last letter. And, like the previous example, the first use anonymous inner class to sort, then use a lambda expression to streamline our code.
In the first example, we will sort list based on name. Using the old way, code is as follows:

String[] players = {"Rafael Nadal", "Novak Djokovic",   
    "Stanislas Wawrinka", "David Ferrer",  
    "Roger Federer", "Andy Murray",  
    "Tomas Berdych", "Juan Martin Del Potro",  
    "Richard Gasquet", "John Isner"};  
   
// 1.1 使用匿名内部类根据 name 排序 players  
Arrays.sort(players, new Comparator<String>() {  
    @Override  
    public int compare(String s1, String s2) {  
        return (s1.compareTo(s2));  
    }  
});

Use lambdas, you can achieve the same function by the following code:

// 1.2 lambda expression sort Players   
Comparator <String> sortByName = (String S1, S2 String) -> (s1.compareTo (S2));   
Arrays.sort (Players, sortByName);   
  
// 1.3 may take the following form:   
Arrays.sort (players, (String s1,   String s2) -> (s1.compareTo (s2)));

Other ordering shown below. As with the above example, the code Comparator are achieved by a number of anonymous inner classes and lambda expressions:

// 1.1 anonymous inner classes sorted according to surname Players   
Arrays.sort (Players, new new Comparator <String> () {   
    @Override   
    public  int Compare (String S1, S2 String) {  
         return (s1.substring (s1.indexOf ( "" .)) the compareTo (s2.substring (s2.indexOf ( "" ))));   
    }   
});   
  
// 1.2 using lambda expression sorted according Surname   
Comparator <String> sortBySurname = (String S1, S2 String) ->    
    ( s1.substring (s1.indexOf ( "")) compareTo (s2.substring (s2.indexOf (. "" ))));   
Arrays.sort (the Players, sortBySurname);   
  
// 1.3 or so, the original author is not suspected wrong, a lot of brackets ...  
Arrays.sort(players, (String s1, String s2) ->   
      ( s1.substring(s1.indexOf(" ")).compareTo( s2.substring(s2.indexOf(" ")) ) )   
    );  
  
// 2.1 使用匿名内部类根据 name lenght 排序 players  
Arrays.sort(players, new Comparator<String>() {  
    @Override  
    public int compare(String s1, String s2) {  
        return (s1.length() - s2.length());  
    }  
});  
  
// 2.2 使用 lambda expression 排序,根据 name lenght  
Comparator<String> sortByNameLenght = (String s1, String s2) -> (s1.length() - s2.length());  
Arrays.sort (Players, sortByNameLenght);   
  
// 2.3 or the this   
Arrays.sort (Players, (S1 String, String S2) -> (s1.length () - s2.length ()));   
  
// 3.1 using anonymous inner class sorting players, according to the last letter   
Arrays.sort (players, new new Comparator <String> () {   
    @Override   
    public  int Compare (String S1, S2 String) {  
         return (s1.charAt (s1.length () -. 1) - s2.charAt (s2.length () -. 1 ));   
    }   
});   
  
// 3.2 sorting using lambda expression, according to the last letter   
Comparator <String> sortByLastLetter =    
    (String S1, S2 String) ->    
        (s1.charAt ( s1.length () -. 1) - S2. the charAt (s2.length () -. 1 ));  
Arrays.sort(players, sortByLastLetter);  
  
// 3.3 or this  
Arrays.sort(players, (String s1, String s2) -> (s1.charAt(s1.length() - 1) - s2.charAt(s2.length() - 1)));

 

Guess you like

Origin www.cnblogs.com/jxd283465/p/11759298.html