java8 Lambda 测试示例
import com.google.gson.Gson; import org.junit.Test; import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; /** * https://www.cnblogs.com/coprince/p/8692972.html */ public class LambdaTest { @Test public void testLambdaTest(){ //用lambda表达式实现Runnable new Thread( () -> System.out.println("In Java8, Lambda expression rocks !!") ).start(); /** * In Java8, Lambda expression rocks !! */ List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API"); //Comparator // List<String> featureSort = features.stream().sorted((a,b) -> (a.compareTo(b))).collect(Collectors.toList()); List<String> featureSort = features.stream().sorted((a,b) -> (a.length() - b.length())).collect(Collectors.toList()); System.out.println("featureSort=" + new Gson().toJson(featureSort)); //使用lambda表达式对列表进行迭代 // features.forEach(n -> System.out.println(n)); // 使用Java 8的方法引用更方便,方法引用由::双冒号操作符标示, // 看起来像C++的作用域解析运算符 方法引用 features.forEach(System.out::println); /** * Lambdas * Default Method * Stream API * Date and Time API */ //使用lambda表达式和函数式接口Predicate 函数式编程 Predicate接口非常适用于做过滤。 //Java 8中使用lambda表达式的Map和Reduce示例 // 使用lambda表达式 List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500); costBeforeTax.stream().map((cost) -> cost + 0.12*cost).forEach(System.out::println); /** * 112.0 * 224.0 * 336.0 * 448.0 * 560.0 */ //Java 8中使用lambda表达式的Map和Reduce示例 // 新方法: List<Integer> costBeforeTax2 = Arrays.asList(100, 200, 300, 400, 500); double bill = costBeforeTax2.stream().map((cost) -> cost + 0.12*cost).reduce((sum, cost) -> sum + cost).get(); System.out.println("Total : " + bill); /** * Total : 1680.0 */ //通过过滤创建一个String列表 // 创建一个字符串列表,每个字符串长度大于2 List<String> filtered = features.stream().filter(x -> x.length()> 2).collect(Collectors.toList()); System.out.printf("Original List : %s, filtered list : %s %n", features, filtered); /** * Original List : [Lambdas, Default Method, Stream API, Date and Time API], filtered list : [Lambdas, Default Method, Stream API, Date and Time API] */ //对列表的每个元素应用函数 // 将字符串换成大写并用逗号链接起来 List<String> 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); /** * USA, JAPAN, FRANCE, GERMANY, ITALY, U.K., CANADA */ //复制不同的值,创建一个子列表 // 用所有不同的数字创建一个正方形列表 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); /** * Original List : [9, 10, 3, 4, 7, 3, 4], Square Without duplicates : [81, 100, 9, 16, 49] */ //计算集合元素的最大值、最小值、总和以及平均值 //获取数字的个数、最小值、最大值、总和以及平均值 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()); /** * Highest prime number in List : 29 * Lowest prime number in List : 2 * Sum of all prime numbers : 129 * Average of all prime numbers : 12.9 */ } }