1. 程式人生 > >Java 高階函式的簡單使用:map,reduce,filter,sorted

Java 高階函式的簡單使用:map,reduce,filter,sorted

().map((x)->(x+100)).forEach((num)->{System.out.print(num+",");}); a.forEach(System.out::print); System.out.println(); } @Test public void testMap1(){ System.out.println("mmmmmmmmmmmmmmmmmmmmmmmmmmm1111"); b = (ArrayList<Integer>) a.stream().map((x)->(x+100
))
.collect(Collectors.toList()); b.forEach(System.out::print); System.out.println(); } @Test public void testReduce(){ System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"); int b = a.stream().reduce((x,y)->x+y).get(); System.out.println("sum = "
+b)
; System.out.println(); } @Test public void testFilter(){ System.out.println("ffffffffffffffffffffffff"); b = (ArrayList<Integer>) a.stream().filter((y)->y%2!=0).collect(Collectors.toList()); b.forEach((x)->System.out.print(x+",")); System
.out.println(); } @Test public void testSort(){ System.out.println("ssssssssssssssssssssssssssss"); b = (ArrayList<Integer>) a.stream().sorted().collect(Collectors.toList()); b.forEach((x)->System.out.print(x+",")); System.out.println(); } @Test public void testSort1(){ System.out.println("ssssssssssssssssssssssssssss11111111"); b = (ArrayList<Integer>) a.stream().sorted((x,y)->{ if(x>y){ return -1; }else if(x==y){ return 0; }else{ return 1; } }).collect(Collectors.toList()); b.forEach((x)->System.out.print(x+",")); System.out.println(); } @Test public void testSort2(){ System.out.println("ssssssssssssssssssssssssssss2222222222222222"); b = (ArrayList<Integer>) a.stream().sorted((x,y)->(x.compareTo(y))).collect(Collectors.toList()); b.forEach((x)->System.out.print(x+",")); System.out.println(); } @Test public void testSort3(){ System.out.println("ssssssssssssssssssssssssssss3333333333333333333"); b = (ArrayList<Integer>) a.stream().sorted((x,y)->(-x.compareTo(y))).collect(Collectors.toList()); b.forEach((x)->System.out.print(x+",")); System.out.println(); } @Test public void testSummaryStatistics(){ System.out.println("================SummaryStatistics===================="); IntSummaryStatistics statistics = a.stream().mapToInt((x)->x).summaryStatistics(); System.out.println("最大值:"+statistics.getMax()); System.out.println("最小值:"+statistics.getMin()); System.out.println("平均值:"+statistics.getAverage()); System.out.println("總數:"+statistics.getCount()); System.out.println("和:"+statistics.getSum()); } @Test public void testParallelStream(){ System.out.println("*******************ParallelStream**********************"); //以下使用有錯誤 // IntStream intStream = a.parallelStream().mapToInt((x)->x); // System.out.println("最大值:"+intStream.max()); // System.out.println("最小值:"+intStream.min()); // System.out.println("平均值:"+intStream.average()); // System.out.println("總數:"+intStream.count()); // System.out.println("和:"+intStream.sum()); System.out.println("最大值:"+a.parallelStream().mapToInt((x)->x).max()); System.out.println("最小值:"+a.parallelStream().mapToInt((x)->x).min()); System.out.println("平均值:"+a.parallelStream().mapToInt((x)->x).average()); System.out.println("總數:"+a.parallelStream().mapToInt((x)->x).count()); System.out.println("和:"+a.parallelStream().mapToInt((x)->x).sum()); } }