Java8新特性——lambda函数式编程

一、遍历循环

 1 /**
 2  * @author jiaqing.xu@hand-china.com
 3  * @version 1.0
 4  * @name
 5  * @description 循环遍历
 6  * @date 2018/7/13
 7  */
 8 public class test1 {
 9     public static void main(String[] args) {
10         String[] atp = {
11                 "b",
12                 "a",
13                 "c",
14                 "d",
15                 "e",
16                 "f",
17                 "g",
18                 "h"};
19         List<String> players = Arrays.asList(atp);
20 
21         // 直接对list进行循环输出
22         players.forEach((p) -> System.out.print(p + "; "));
23         System.out.println("\n");
24         //循环输出带换行符,方法引用使用双冒号
25         players.forEach(System.out::println);
26 
27     }
28 }

二、数据过滤

 1 /**
 2  * @author jiaqing.xu@hand-china.com
 3  * @version 1.0
 4  * @name
 5  * @description 数据过滤
 6  * @date 2018/7/13
 7  */
 8 public class test2 {
 9     public static void main(String args[]) {
10         List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
11         //打印以J开头的字符串
12         System.out.println("Print all languages :");
13         filter1(languages, (str) -> str.toString().startsWith("J"));
14         //条件恒为假
15         System.out.println("Print no language : ");
16         filter(languages, (str) -> false);
17         //混合过滤条件
18         System.out.println("...");
19         filter3(languages);
20     }
21 
22     //传统过滤
23     public static void filter(List<String> names, Predicate condition) {
24         for (String name : names) {
25             if (condition.test(name)) {
26                 System.out.println(name + " ");
27             }
28         }
29     }
30 
31     //以lambda方式过滤
32     public static void filter1(List names, Predicate condition) {
33         names.stream().filter((name) -> (condition.test(name))).forEach((name) -> System.out.println(name + " "));
34     }
35 
37     public static void filter3(List names) {
38         //第一个规则是以J开头
39         Predicate<String> startsWithJ = (n) -> n.startsWith("J");
40         //第二个规则是长度为4
41         Predicate<String> fourLetterLong = (n) -> n.length() == 4;
42         names.stream().filter(startsWithJ.and(fourLetterLong)).forEach((n) -> System.out.println("The result is:" + n));
43     }
45 }

三、Map和Reduce函数计算

 1 /**
 2  * @author jiaqing.xu@hand-china.com
 3  * @version 1.0
 4  * @name
 5  * @description map和reduce 函数计算
 6  * @date 2018/7/13
 7  */
 8 public class test3 {
 9     public static void main(String[] args) {
10         // With Lambda expression:
11         //Map用于函数计算,为集合中的每个元素增加一定的数值
12         List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
13         costBeforeTax.stream().map((cost) -> (Integer) cost + .12 * (Integer) cost).forEach(System.out::println);
14 
15         //reduce 类似sql中的sum avg count
16         List costBeforeTax2 = Arrays.asList(100, 200, 300, 400, 500);
17         Object bill = costBeforeTax2.stream()
18                 .map((cost) -> (Integer) cost + .12 * (Integer) cost)
19                 .reduce((sum, cost) -> (Double) sum + (Double) cost)
20                 .get();
21         System.out.println("Total : " + bill);
22 
23         //应用函数将字符串转换为大写形式并用逗号拼接
24         List<String> G7 = Arrays.asList("USA", "Japan", "France", "Germany", "Italy", "U.K.", "Canada");
25         String G7Countries = G7.stream()
26                 .map(x -> x.toUpperCase())
27                 .collect(Collectors.joining(", "));
28         System.out.println(G7Countries);
29 
30 
31         //计算list中最大值、最小值和平均值
32         List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
33         IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x)
34                 .summaryStatistics();
35         System.out.println("Highest prime number in List : " + stats.getMax());
36         System.out.println("Lowest prime number in List : " + stats.getMin());
37         System.out.println("Sum of all prime numbers : " + stats.getSum());
38         System.out.println("Average of all prime numbers : " + stats.getAverage());
39     }
40 }

 

posted @ 2018-07-13 20:20  XuJiaqing  阅读(375)  评论(0编辑  收藏  举报