深刻理解Java8 Lambda表達式

1、Lambda表達式是什麼?

Lambda表達式有兩個特色:一是匿名函數;二是可傳遞。

匿名函數的應用場景是:

一般在須要一個函數,但又不想費神去命名一個函數的場合下使用。Lambda表達式所表示的匿名函數的內容應該是很簡單的,若是複雜的話,乾脆就去從新定義一個函數了。多線程

可傳遞的應用場景是:

就是將Lambda表達式做爲參數傳遞給其餘函數,Lambda表達式做爲一種更緊湊的代碼風格,使Java的語言表達能力能到提高。函數

2、Lambda表達式語法

Lambda表達式在Java語言中引入了一個新的語法元素和操做符。這個操做符是「->」,該操做符被稱爲Lambda操做符或箭頭操做符,它將Lambda分爲兩個部分:spa

左側:指定了Lambda表達式所須要的全部參數線程

右側:制定了Lambda體,即Lambda表達式所要執行的功能。對象

3、Lambda表達式常見的語法格式

3.一、無參、無返回值,Lambda體只須要一條語句。

Runnable r = () -> System.out.println("Hello Lambda!");

3.二、Lambda須要一個參數

Consumer<String> con = (x) -> System.out.println(x);

3.三、Lambda只須要一個參數時,參數的小括號能夠省略

Consumer<String> con = x -> System.out.println(x);

3.四、Lambda須要兩個參數,而且有返回值

Comparator<Integer> com = (x, y) -> {接口

System.out.println("函數式接口");開發

return Integer.compare(x, y);字符串

};get

3.五、當Lambda體只有一條語句時,return與大括號能夠省略

Comparator<Integer> com = (x, y) -> Integer.compare(x, y);

3.六、數據類型能夠省略,由於可由編譯器推斷得出,稱爲類型推斷

BinaryOperator<Long> operator = (Long x, Long y) -> {編譯器

System.out.println("實現函數接口方法");

return x + y;

};

4、Lambda表達式實戰

4.一、實現一個多線程

new Thread(() -> System.out.println("In Java8!")).start();

4.二、遍歷list集合

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
// 直接打印
list.forEach(System.out::println);

// 取值分別操做
list.forEach(i -> {
    System.out.println(i * 3);
});

4.三、map函數,容許將對象進行轉換。好比,能夠更改list中的每一個元素的值

List< Integer > list = Arrays.asList(1, 2, 3);
// 可改變對象
list.stream().map((i) -> i * 3).forEach(System.out::println);

// 不可改變原有對象
list.forEach(i -> i = i * 3);
list.forEach(System.out::println);

4.四、reduce函數,用來將值進行合併,map和reduce函數式函數式變成的核心。

List< Integer > list = Arrays.asList(1, 2, 3);
Integer integer = list.stream().map((i) -> i = i * 3).reduce((sum, count) -> sum += count).get();
System.out.println(integer);

reduce的更多用法

// 字符串鏈接,concat = "ABCD"
String concat = Stream.of("A", "B", "C", "D").reduce("", String::concat);

// 求最小值,minValue = -3.0
double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min);

// 求和,sumValue = 10, 有起始值
int sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);

// 求和,sumValue = 10, 無起始值
sumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();

// 過濾,字符串鏈接,concat = "ace"
concat = Stream.of("a", "B", "c", "D", "e", "F").filter(x -> x.compareTo("Z") > 0).reduce("", String:concat);

4.五、過濾

過濾是Java開發者在大規模集合上的一個經常使用操做,而如今使用lambda表達式和流API過濾大規模數據集合是驚人的簡單。流提供了一個 filter() 方法,接受一個 Predicate 對象,便可以傳入一個lambda表達式做爲過濾邏輯。下面的例子是用lambda表達式過濾Java集合,將幫助理解。

List<String> strList = Arrays.asList("abc", "eqwr", "bcd", "qb" , "ehdc", "jk");
List<String> filtered = strList.stream().filter(x -> x.length()> 2).collect(Collectors.toList());
System.out.printf("Original List : %s, filtered list : %s %n", strList, filtered);

4.六、Predicate接口

Predicate是jdk8中的新增接口,共有5個方法。

and(Predicate<? super T> p)  negate()  or(Predicate<? super T> p)  test(T t)  xor(Predicate<? super T> p)

該接口除了test方法是抽象方法, 其他都是default方法, 該接口可接受一個 lambda表達式, 其實就是實現了test接口的一個匿名類

public static void main(String[] args) {
    List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
    System.out.println("Language which starts with J :");
    filter(languages, (str) -> ((String) str).startsWith("J"));

    System.out.println("Language which ends with a :");
    filter(languages, (str) -> ((String) str).endsWith("a"));

    System.out.println("Print all languages :");
    filter(languages, (str) -> true);

    System.out.println("Print no language :");
    filter(languages, (str) -> false);

    System.out.println("Print language whose length greater than 4:");
    filter(languages, (str) -> ((String) str).length() > 4);


    Predicate<String> startWithJ = (n) -> n.startsWith("J");
    Predicate<String> fourLength = (n) -> n.length() == 4;
    languages.stream().filter(startWithJ.and(fourLength)).forEach(System.out::println);
}

public static void filter(List<String> names, Predicate condition) {
    names.stream().filter(x -> condition.test(x)).forEach(x -> System.out.println(x + ""));
}

4.七、對列表的每一個元素使用函數

List<String> strList = Arrays.asList("abc", "eqwr", "bcd", "qb" , "ehdc", "jk");
String collect = strList.stream().map(x -> x.toUpperCase()).collect(Collectors.joining(", "));
System.out.printf("filtered list : %s %n",  collect);

4.八、使用distinct進行去重

List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 7, 3, 4);
List<Integer> distinct = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, distinct);

4.九、計算最值和平均值

IntStream、LongStream 和 DoubleStream 等流的類中,有個很是有用的方法叫作 summaryStatistics() 。能夠返回 IntSummaryStatistics、LongSummaryStatistics 或者 DoubleSummaryStatistic s,描述流中元素的各類摘要數據。在本例中,咱們用這個方法來計算列表的最大值和最小值。它也有 getSum() 和 getAverage() 方法來得到列表的全部元素的總和及平均值。

//獲取數字的個數、最小值、最大值、總和以及平均值
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("Highest prime number in List : " + stats.getMax());
System.out.println("Lowest prime number in List : " + stats.getMin());
System.out.println("Sum of all prime numbers : " + stats.getSum());
System.out.println("Average of all prime numbers : " + stats.getAverage());
相關文章
相關標籤/搜索