1、遍歷循環sql
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 }
2、數據過濾express
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 }
3、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 }