• Spliterator介绍


    一、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.
    " +
     14                     "
    " +
     15                     "Integer dictum, erat ut interdum viverra, erat dui suscipit eros, eget vestibulum mi massa ut sem. Donec ultrices.
    " +
     16                     "
    " +
     17                     "Quisque lacinia non enim venenatis efficitur. Duis luctus at ex at ullamcorper. Fusce maximus sapien a scelerisque.
    " +
     18                     "
    " +
     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("
    ");
     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.
    

      

    ---

  • 相关阅读:
    河北省重大技术需求征集系统(10)
    河北省重大技术需求征集系统(9)
    大三上学期周总结
    河北省重大技术需求征集系统(8)
    《代码大全》阅读笔记(三)
    河北省重大技术需求征集系统(7)
    河北省重大技术需求征集系统(6)
    河北省重大技术需求征集系统(5)
    【自动化】Aritest+python实现客户端UI自动化
    【自动化】Monkey自动化测试实现总结
  • 原文地址:https://www.cnblogs.com/tenWood/p/11602092.html
Copyright © 2020-2023  润新知