Java 8 Streams Java Java API
Class:
java.util.stream.StreamSupport
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); } }
Output14
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); } }
Output4.666666666666667
|
|