1 public static void main(String[] args) { 2 // ‘代替匿名类’ 3 // new Thread(() -> System.out.println("java8 lambda")).start(); 4 5 // ‘list 遍历集合’ 6 List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API"); 7 // features.forEach(n -> System.out.println(n)); 8 9 // ‘函数接口 filter’ 10 List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp"); 11 Predicate<String> predicate = str -> str.equals("Java"); 12 // languages.stream().filter(n -> predicate.test((String) n)).forEach(n -> System.out.println(n)); 13 //’通过filtering 创建一个字符串String的集合,需要找个变量接受一下 14 List<String> tempString = (List<String>) languages.stream().filter(n -> predicate.test((String) n)).collect(Collectors.toList()); 15 // tempString.forEach(System.out::println); 16 //map允许你改变你内部元素 17 List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500); 18 // costBeforeTax.stream().map((n) -> n + n * 12).forEach(n -> System.out.println(n)); 19 //reduce 可以理解成 sql的函数 20 Integer tempSum = costBeforeTax.stream().map((n) -> n + n * 12).reduce((sum,n1)->sum + n1).get(); 21 //summaryStatistics包含最大值最小值,总和及平均值 22 IntSummaryStatistics stats = costBeforeTax.stream().mapToInt(n->n).summaryStatistics(); 23 System.out.println("Highest prime number in List : " + stats.getMax()); 24 System.out.println("Lowest prime number in List : " + stats.getMin()); 25 System.out.println("Sum of all prime numbers : " + stats.getSum()); 26 System.out.println("Average of all prime numbers : " + stats.getAverage()); 27 //function函数Function<T,R> 输入T返回R 28 Function<String,String> function = n-> n + "jack"; 29 System.out.println(function.apply("String")); 30 }