Close

Java 8 Streams - StreamSupport.longStream() 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 LongStream longStream(Spliterator.OfLong spliterator,
                                    boolean parallel)

Creates a new sequential or parallel LongStream from a Spliterator.OfLong.



public static LongStream longStream(Supplier<? extends Spliterator.OfLong> supplier,
                                    int characteristics,
                                    boolean parallel)

Creates a new sequential or parallel LongStream from a Supplier of Spliterator.OfLong . 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.LongStream;
import java.util.stream.StreamSupport;

public class LongStreamExample {

public static void main(String... args) {
LongStream ls = StreamSupport.longStream(Arrays.spliterator(new long[]{1L, 5L, 8L}), false);
long sum = ls.sum();
System.out.println(sum);
}
}

Output

14




package com.logicbig.example.streamsupport;

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

public class LongStreamExample2 {

public static void main(String... args) {
LongStream ls = StreamSupport.longStream(() -> Arrays.spliterator(new long[]{1L, 5L, 8L}),
Spliterator.DISTINCT, false);
double avg = ls.average().orElse(0);
System.out.println(avg);
}
}

Output

4.666666666666667




See Also