@Test publicvoidFilterTest()throws IOException { //Stream<T> filter(Predicate<? super T> predicate) //产生一个流,它包含当前流中所有满足谓词条件的元素 var contents = new String(Files.readAllBytes(Paths.get("words.txt")), StandardCharsets.UTF_8); Stream<String> words = Stream.of(contents.split("\\PL+")); Stream<String> stream = words.filter(w -> w.length() > 12); }
map方法
1 2 3 4 5 6 7 8
@Test publicvoidMapTest()throws IOException { //<R> Stream<R> map(Function<? super T,? extends Stream<? extends R>>mapper) //产生一个流,它包含将mapper应用于当前流中所有元素所产生的结果。 var contents = new String(Files.readAllBytes(Paths.get("words.txt")), StandardCharsets.UTF_8); Stream<String> words = Stream.of(contents.split("\\PL+")); Stream<String> stream = words.map(String::toLowerCase); }
flatMap方法
1 2 3 4 5 6 7 8
@Test publicvoidFlatMapTest()throws IOException { //<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper) //产生一个流,它是通过将mapper应用于当前流中所有元素所参数的结果连接到一起获得的。(注意,这里的每个结果都是一个流) var contents = new String(Files.readAllBytes(Paths.get("words.txt")), StandardCharsets.UTF_8); Stream<String> words = Stream.of(contents.split("\\PL+")); Stream<String> stream = words.flatMap(w->Stream.of(w.toLowerCase())); }
小测试
对于filter和map方法,我们来进行一个小测试:
1 2 3 4 5 6 7 8 9 10 11
@Test publicvoidTest1(){ //每次创建的stream只能使用一次,如果连续执行一个对象的stream 就会报错: // java.lang.IllegalStateException: stream has already been operated upon or closed
//例如,letters("hello")的返回值是流["h","e","l","l","o"] publicstatic Stream<String> letters(String s){ ArrayList<String> res = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { res.add(s.substring(i,i+1)); } return res.stream(); }
//4.collect方法 @Test publicvoidTest4(){ //<R, A> R collect(Collector<? super T, A, R> collector) //使用给定的收集器来收集当前流中的元素。Collectors类有用于多种收集器的工厂方法。 Stream<Integer> stream = Stream.iterate(1, n -> n + 1).limit(10); List<Integer> list = stream.collect(Collectors.toList()); System.out.println(list); }
summarizing方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//5.summarizing方法 @Test publicvoidTest5(){ //static <T> Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) //static <T> Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper) //static <T> Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) //产生能够生成(Int|Long|Double)SummaryStatistics对象的收集器,通过它们可以获得将mapper应用于每个元素后所产生的结果的数量,总和,平均值,最大值和最小值。 IntSummaryStatistics summary = Stream.iterate(1, n -> n + 1).limit(10).collect(Collectors.summarizingInt(x -> x)); //在得到SummaryStatistics对象的收集器后,可以通过以下方法求取值。 long count = summary.getCount();//10 int max = summary.getMax();//10 int min = summary.getMin();//1 double average = summary.getAverage();//5.5 long sum = summary.getSum();//55 }
@Test publicvoidTest4(){ long start = System.currentTimeMillis(); long sum = 0; for (long i = 0L; i < 10000000000L; i++) { sum+=i; } System.out.println(sum); long end = System.currentTimeMillis(); System.out.println("耗费时间:"+(end-start));//大概5500左右 }
运行结果:
接着我们用普通串行流进行测试:
1 2 3 4 5 6 7 8
@Test publicvoidTest3(){ long start = System.currentTimeMillis(); long sum = LongStream.range(0, 10000000000L).sum(); System.out.println(sum); long end = System.currentTimeMillis(); System.out.println("耗费时间:"+(end-start));//大概4400左右 }
运行结果:
最后我们用并行流进行测试:
1 2 3 4 5 6 7 8
@Test publicvoidTest2(){ long start = System.currentTimeMillis(); long sum = LongStream.range(0, 10000000000L).parallel().sum(); System.out.println(sum); long end = System.currentTimeMillis(); System.out.println("耗费时间:"+(end-start));//大概1800左右 }