Spliterator介绍

[亡魂溺海] 提交于 2019-11-30 15:50:35

一、Spliterator    

Spliterator<T> trySplit();
trySplit接口就相当于我们之前写的AccumulatorRecursiveTask类里面的分割任务,如果满足条件就继续往下分,返回一个新的spliterator出来。
Spliterator就相当于我们写的fork join逻辑,Stream就是使用Spliterator来实现fork join的功能,来实现多任务切分的。

代码例子:

  1 package com.cy.java8;
  2 
  3 import java.util.Objects;
  4 import java.util.Optional;
  5 import java.util.Spliterator;
  6 import java.util.function.Consumer;
  7 import java.util.stream.IntStream;
  8 import java.util.stream.Stream;
  9 import java.util.stream.StreamSupport;
 10 
 11 public class SpliteratorInAction {
 12     private static String text =
 13                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in lacinia massa, quis eleifend dui. Sed in molestie ex.\n" +
 14                     "\n" +
 15                     "Integer dictum, erat ut interdum viverra, erat dui suscipit eros, eget vestibulum mi massa ut sem. Donec ultrices.\n" +
 16                     "\n" +
 17                     "Quisque lacinia non enim venenatis efficitur. Duis luctus at ex at ullamcorper. Fusce maximus sapien a scelerisque.\n" +
 18                     "\n" +
 19                     "Morbi eu congue ipsum, ac lobortis nulla. Proin cursus tortor lectus, nec luctus lectus ornare a. Mauris gravida nisl.";
 20 
 21     public static void main(String[] args) {
 22         IntStream intStream = IntStream.rangeClosed(0, 10);
 23         Spliterator.OfInt spliterator = intStream.spliterator();
 24         spliterator.forEachRemaining((Consumer<Integer>) integer -> System.out.println(integer));
 25 
 26         MySpliteratorText mySpliteratorText = new MySpliteratorText(text);
 27         Optional.ofNullable(mySpliteratorText.stream().count())
 28                 .ifPresent(System.out::println);
 29         mySpliteratorText.stream().filter(s -> !s.equals("")).forEach(System.out::println);
 30 
 31         Optional.ofNullable(mySpliteratorText.parallelStream().count())
 32                 .ifPresent(System.out::println);
 33         mySpliteratorText.parallelStream().filter(s -> !s.equals("")).forEach(System.out::println);
 34     }
 35 
 36     /**
 37      * 自定义实现Spliterator接口
 38      */
 39     public static class MySpliteratorText {
 40         private final String[] data;
 41 
 42         public MySpliteratorText(String text) {
 43             Objects.requireNonNull(text, "the parameter can not be null");
 44             this.data = text.split("\n");
 45         }
 46 
 47         public Stream<String> stream() {
 48             return StreamSupport.stream(new MySpliterator(), false);
 49         }
 50 
 51         public Stream<String> parallelStream() {
 52             return StreamSupport.stream(new MySpliterator(), true);
 53         }
 54 
 55         private class MySpliterator implements Spliterator<String> {
 56             private int start, end;
 57 
 58             public MySpliterator() {
 59                 this.start = 0;
 60                 this.end = data.length - 1;
 61             }
 62 
 63             public MySpliterator(int start, int end) {
 64                 this.start = start;
 65                 this.end = end;
 66             }
 67 
 68             @Override
 69             public boolean tryAdvance(Consumer<? super String> action) {
 70                 if (start <= end) {
 71                     action.accept(data[start++]);
 72                     return true;
 73                 }
 74                 return false;
 75             }
 76 
 77             @Override
 78             public Spliterator<String> trySplit() {
 79                 int mid = (end - start) / 2;
 80                 if (mid <= 1) {
 81                     return null;
 82                 }
 83 
 84                 int left = start;
 85                 int right = start + mid;
 86                 start = start + mid + 1;
 87                 return new MySpliterator(left, right);
 88             }
 89 
 90             @Override
 91             public long estimateSize() {
 92                 return end - start;
 93             }
 94 
 95             @Override
 96             public long getExactSizeIfKnown() {
 97                 return estimateSize();
 98             }
 99 
100             @Override
101             public int characteristics() {
102                 return IMMUTABLE | SIZED | SUBSIZED;
103             }
104         }
105     }
106 }

console打印:

0
1
2
3
4
5
6
7
8
9
10
7
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in lacinia massa, quis eleifend dui. Sed in molestie ex.
Integer dictum, erat ut interdum viverra, erat dui suscipit eros, eget vestibulum mi massa ut sem. Donec ultrices.
Quisque lacinia non enim venenatis efficitur. Duis luctus at ex at ullamcorper. Fusce maximus sapien a scelerisque.
Morbi eu congue ipsum, ac lobortis nulla. Proin cursus tortor lectus, nec luctus lectus ornare a. Mauris gravida nisl.
7
Quisque lacinia non enim venenatis efficitur. Duis luctus at ex at ullamcorper. Fusce maximus sapien a scelerisque.
Morbi eu congue ipsum, ac lobortis nulla. Proin cursus tortor lectus, nec luctus lectus ornare a. Mauris gravida nisl.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in lacinia massa, quis eleifend dui. Sed in molestie ex.
Integer dictum, erat ut interdum viverra, erat dui suscipit eros, eget vestibulum mi massa ut sem. Donec ultrices.

  

 

---

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!