转载地址:https://blog.csdn.net/rongrong_love_lc/article/details/72845528
备份下:
package test;import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.stream.Collectors;import org.junit.Test;public class LambdaTest {static ArrayList<Integer> a = new ArrayList<Integer>();static ArrayList<Integer> b = null;static {a.add(4);a.add(3);a.add(2);a.add(1);a.add(6);a.add(7);a.add(8);a.add(5);a.add(9);a.add(0);}@Testpublic void testMap_forEach() {System.out.println("mmmmmmmmmmmmmmmmmmmmmmmmmmm");a.stream().map((x) -> (x + 100)).forEach((num) -> {System.out.print(num + ",");});a.forEach(System.out::print);System.out.println();}@Testpublic void testMap_collect() {System.out.println("mmmmmmmmmmmmmmmmmmmmmmmmmmm1111");b = (ArrayList<Integer>) a.stream().map((x) -> (x + 100)).collect(Collectors.toList());a.forEach(System.out::print);System.out.println();b.forEach(System.out::print);}@Testpublic void testReduce() {System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");int b = a.stream().reduce((x, y) -> x + y).get();a.stream().forEach(System.out::print);System.out.println();System.out.println("sum = " + b);}@Testpublic 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();}@Testpublic 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();}@Testpublic 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();}@Testpublic 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();}@Testpublic 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();}@Testpublic 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());}@Testpublic 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());}}