Java Stream Spliterator try to split

Introduction

The following method:

default void forEachRemaining(Consumer<? super T> action) 

applies action to each unprocessed element and then returns.

trySplit() splits the elements being iterated in two, returning a new Spliterator to one of the partitions.

Spliterator<T> trySplit() 

The other partition remains accessible by the original Spliterator.

If it is not possible to split the invoking Spliterator, null is returned.

import java.util.ArrayList;
import java.util.Spliterator;
import java.util.stream.Stream;

public class Main {

  public static void main(String[] args) {

    // Create a list of Strings.
    ArrayList<String> myList = new ArrayList<>();
    myList.add("Alpha");
    myList.add("Beta");
    myList.add("Gamma");
    myList.add("Delta");
    myList.add("Phi");
    myList.add("Omega");

    // Obtain a Stream to the array list.
    Stream<String> myStream = myList.stream();

    // Obtain a Spliterator.
    Spliterator<String> splitItr = myStream.spliterator();

    // Now, split the first iterator.
    Spliterator<String> splitItr2 = splitItr.trySplit();

    // If splitItr could be split, use splitItr2 first.
    if (splitItr2 != null) {
      System.out.println("Output from splitItr2: ");
      splitItr2.forEachRemaining((n) -> System.out.println(n));
    }/*from  ww w .  j  a v a2s .  c om*/

    // Now, use the splitItr.
    System.out.println("\nOutput from splitItr: ");
    splitItr.forEachRemaining((n) -> System.out.println(n));
  }
}



PreviousNext

Related