Close

Java 8 Streams - StreamSupport.intStream() Examples

Java 8 Streams Java Java API 


Class:

java.util.stream.StreamSupport

java.lang.Objectjava.lang.Objectjava.util.stream.StreamSupportjava.util.stream.StreamSupportLogicBig

Methods:

public static IntStream intStream(Spliterator.OfInt spliterator,
                                  boolean parallel)

Creates a new sequential or parallel IntStream from a Spliterator.OfInt.



public static IntStream intStream(Supplier<? extends Spliterator.OfInt> supplier,
                                  int characteristics,
                                  boolean parallel)

Creates a new sequential or parallel IntStream from a Supplier of Spliterator.OfInt . This method also specifies the characteristics of the supplied Spliterator.

See also Iterator vs Spliterator.


Examples


package com.logicbig.example.streamsupport;

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

public class IntStreamExample {

public static void main(String... args) {
IntStream ds = StreamSupport
.intStream(Arrays.spliterator(new int[]{1, 3, 6}), false);
double sum = ds.sum();
System.out.println(sum);
}
}

Output

10.0




package com.logicbig.example.streamsupport;

import java.util.Arrays;
import java.util.Spliterator;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

public class IntStreamExample2 {

public static void main(String... args) {
int[] array = {1, 3, 6};
Spliterator.OfInt spliterator = Arrays.spliterator(array);
IntStream ds = StreamSupport
.intStream(() -> spliterator, Spliterator.ORDERED, false);
double sum = ds.sum();
System.out.println(sum);
}
}

Output

10.0




See Also