转自: http://mp.weixin.qq.com/s/Xhr9aNEMr0fIUWh27mH1pw


package com.beck.nt;


import java.util.Arrays;

import java.util.IntSummaryStatistics;

import java.util.List;

import java.util.function.Predicate;

import java.util.stream.Collectors;


public class TestLambda {


//示例,

public static void main(String[] args) {

new Thread( ()-> System.out.println("I am a lambda expression.") ).start();

System.out.println("\n ----------testLambda2 ---------------") ;

testLambda2() ;

System.out.println("\n ----------testLambda3 ---------------") ;

testLambda3() ;

System.out.println("\n ----------testLambda5 ---------------") ;

testLambda5()   ;

System.out.println("\n ----------testLambda6MapAndReduce ---------------") ;

testLambda6MapAndReduce();

System.out.println("\n ----------testLambda6MapAndReduce_demo2 ---------------") ;

testLambda6MapAndReduce_demo2() ;

System.out.println("\n ----------testLambda7 ---------------") ;

testLambda7() ;

System.out.println("\n ----------testLambda8 ---------------") ;

testLambda8();

System.out.println("\n ----------testLambda9 ---------------") ;

testLambda9();

System.out.println("\n ----------testLambda10 ---------------") ;

testLambda10();

}

//predicate函数1

public static void testLambda2(){

List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");

for (String feature : features) {

    System.out.println(feature);

}

System.out.println(" ----------testLambda2 ---- again -----------") ;

features.forEach( System.out::println);

}

//predicate函数2

public static void testLambda3(){

List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");

System.out.println("----------Languages which starts with J :");

filter(languages, (str)->str.startsWith("J"));

System.out.println("----------Languages which ends with a ");

    filter(languages, (str)->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)->str.length() > 4);

}

public static void filter(List names, Predicate condition) {

    for(String name: names)  {

        if(condition.test(name)) {

            System.out.println(name + " ");

        }

    }

}

//实现hadoop map-reduce (1)

public static void testLambda5(){

List languages = Arrays.asList("Java", "Scala", "C++", "JHal", "Lisp");

Predicate startsWithJ = (n) -> n.startsWith("J");

Predicate fourLetterLong = (n) -> n.length() == 4;

//languages.stream().filter(startsWithJ.and(fourLetterLong)).forEach((n) -> System.out.print("nName, which starts with 'J' and four letter long is : " + n));

languages.stream().filter(startsWithJ).filter(fourLetterLong).forEach((n) -> System.out.print("nName, which starts with 'J' and four letter long is : " + n));

}

//实现hadoop map-reduce (2)

public static void testLambda6MapAndReduce(){

List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);

for (Integer cost : costBeforeTax) {

    double price = cost + .12 * cost;

    System.out.println(price);

}

System.out.println();

// 使用lambda表达式

List costBeforeTax2 = Arrays.asList(100, 200, 300, 400, 500);

costBeforeTax2.stream().map((cost) -> cost + .12 * cost).forEach(System.out::println);

}

//求和

public static void testLambda6MapAndReduce_demo2(){

// new method

List costBeforeTax2 = Arrays.asList(100, 200, 300, 400, 500);

double bill = costBeforeTax2.stream().map((cost) -> cost + .12*cost).reduce((sum, cost) -> sum + cost).get();

System.out.println("Total : " + bill);

}

// 过滤

public static void testLambda7(){

List strList = Arrays.asList("USA", "Japan", "France",  "U.K.","Canada") ;

List filtered = strList.stream().filter(x -> x.length()> 2).collect(Collectors.toList());

System.out.printf("Original List : %s, filtered list : %s %n", strList, filtered);

}

//(1) 大小写转换,

//(2) List 转  字符串

public static void testLambda8(){

List G7 = Arrays.asList("USA", "Japan", "France", "Germany", "Italy", "U.K.","Canada");

String G7Countries = G7.stream().map(x -> x.toUpperCase()).collect(Collectors.joining(", "));

System.out.println(G7Countries);

}

//去重 , 利用流的 distinct() 方法来对集合进行去重

public static void testLambda9(){

List numbers = Arrays.asList(9, 10, 3, 4, 7, 3, 4);

List distinct = numbers.stream().map( i -> i * i).distinct().collect(Collectors.toList());

System.out.printf("Original List : %s,  Square Without duplicates : %s %n", numbers, distinct);

}

//最大/小,平均,和

//IntStream、LongStream 和 DoubleStream 等流的类中,有个非常有用的方法叫做 summaryStatistics() 

//可以返回 IntSummaryStatistics、LongSummaryStatistics 或者 DoubleSummaryStatistic

public static void testLambda10(){

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

}

}