Java Lambda 表达式的几各形式

f663x 10年前

一、Lambda表达式的组成主要有传入参数列表(参数1,参数2,...),箭头 ->,及返回值,可以是某个值,或者结构体。

如下举一个小小的例子:

在没有使用Lambda表达式时:


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();      }  }



加入了Lambda表达式后可以写成(下面以3种方法实现):



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方法一          //Arrays.sort(dogs,(Dog m,Dog n)->Integer.compare(m.getWeight(), n.getWeight()));//Lambda方法二          //printDogs(dogs);                    //方法三          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();      }  }



看完后是不是很激动。


下面再放几个例子,参才其实现方式:


import java.util.Arrays;    /**   *   * @author Kangjun   */  public class StandardSyntaxLambda {     private static final String[] arr = {"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 String[] 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));      }    }



在jkd8中Java加入了激动人心的函数式编程方法Lambda表达式,这使得代码更加简洁。