Java Code Examples for java.util.stream.LongStream

Following code examples demonstrate how to use java.util.stream.LongStreamfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.util.stream.LongStreamand various code implementation of this class.

    public static void main(String[] args) {
        long[] arr = {11L, 12, 28, 496, 220, 1184,  12496, 1264460,
                           790, 909, 562, 1064, 1488};

        LongStream.rangeClosed(1, 10).forEach(n -> aliquot(n, 16, 1L << 47));
        System.out.println();
        Arrays.stream(arr).forEach(n -> aliquot(n, 16, 1L << 47));
    } 


    private LongStream allPrimes(Long n) {
        return rangeClosed(2, (long) Math.sqrt(n.doubleValue()))
                .filter(x -> isPrime(x));
    } 

    private List<Long> primesFactor(Long n, LongStream primes, int count) {

        if (isPrime(n)) {
            return asList(n);
        }

        PrimitiveIterator.OfLong remainingPrimes = primes.iterator();
        if (!remainingPrimes.hasNext()){
            return emptyList();
        }

        Long currentPrime = remainingPrimes.next();
        if (n % currentPrime == 0) {
            List<Long> result = new ArrayList<>();
            result.add(currentPrime);
            result.addAll(primesFactor(n / currentPrime, allPrimes(n), 0));
            return result;
        }

        return primesFactor(n, allPrimes(n).skip(++count), count);
    } 

    private List<Long> getPrimeList(Long upperLimit) {
        LongStream longStream = rangeClosed(2, upperLimit).parallel();
        double maxDivisor = Math.sqrt(upperLimit);
        for(long d = 2; d < maxDivisor; d++){
            long divisor = d;
            longStream = longStream.filter(i -> (i <= divisor || i % divisor > 0));
        }
        return longStream.boxed()
                .collect(Collectors.toList());

    } 

    protected LongStream getRangeCandidates(long upperLimit) {
        LongStream longStream = rangeClosed(2, upperLimit)
                .filter(i -> (i <= 2 || i % 2 > 0))
                .filter(i -> (i <= 3 || i % 3 > 0))
                .filter(i -> (i <= 5 || i % 5 > 0))
                .filter(i -> (i <= 7 || i % 7 > 0))
                .filter(i -> (i <= 11 || i % 11 > 0));
        return longStream;
    } 

	private static LongStream primeFactors(final long number) {
		if (number <= 1L)
			return empty();

		final Long factor = rangeClosed(2, round(sqrt(number)))
				.parallel()
				.filter(possiblePrimeFactor -> isDivisibleBy(number, possiblePrimeFactor))
				.findFirst()
				.orElse(number);

		return concat(of(factor), primeFactors(number / factor));
	} 

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to LongStream.class!",
                   LongStream.class.isAssignableFrom(sut));
    } 

        if (s instanceof Spliterator.OfLong) {
            return (Spliterator.OfLong) s;
        } else {
            if (java.util.stream.Tripwire.ENABLED)
                java.util.stream.Tripwire.trip(AbstractPipeline.class,
                              "using LongStream.adapt(Spliterator<Long> s)");
            throw new UnsupportedOperationException("LongStream.adapt(Spliterator<Long> s)");
        } 

        if (sink instanceof LongConsumer) {
            return (LongConsumer) sink;
        } else {
            if (java.util.stream.Tripwire.ENABLED)
                java.util.stream.Tripwire.trip(AbstractPipeline.class,
                              "using LongStream.adapt(Sink<Long> s)");
            return sink::accept;
        } 

	public SELF concat(SELF after) {
		Objects.requireNonNull(after);
		return asSELF(s -> LongStream.concat(s, after.maker().get()));
	} 

Advertisement
Javadoc
A sequence of primitive long-valued elements supporting sequential and parallel aggregate operations

. This is the long primitive specialization of Stream.

The following example illustrates an aggregate operation using Stream and LongStream, computing the sum of the weights of the red widgets: long sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToLong(w -> w.getWeight()) .sum(); See the class documentation for Stream and the package documentation for java.util.stream for additional specification of streams, stream operations, stream pipelines, and parallelism. @since 1.8 @see Stream @see java.util.stream

Read More
Advertisement