Java Stream Operation gcd(IntStream numbers)

Here you can find the source of gcd(IntStream numbers)

Description

Calculate the greatest commom divisor of all numbers in the int stream

License

Open Source License

Parameter

Parameter Description
numbers a int stream with some numbers

Return

the greatest common divisor of the numbers in the int stream

Declaration

public static int gcd(IntStream numbers) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    private static int gcd(int x, int y) {
        return (y == 0) ? x : gcd(y, x % y);
    }//  w ww  . j  ava  2s.c om

    /**
     * Calculate the greatest commom divisor of all numbers in the int stream
     * 
     * @param numbers
     *            a int stream with some numbers
     * @return the greatest common divisor of the numbers in the int stream
     */
    public static int gcd(IntStream numbers) {
        return numbers.reduce(0, (x, y) -> gcd(x, y));
    }

    /**
     * Calculate the greatest commom divisor of some integers
     * 
     * @param numbers
     *            as many integers as you like
     * @return the greatest common divisor of the given numbers
     */
    public static int gcd(int... numbers) {
        return gcd(Arrays.stream(numbers));
    }
}

Related

  1. findLastOf(Stream stream)
  2. findStreamAmongst(Class clazz, Collection instances)
  3. firstValue(Stream stream)
  4. flatOptionals(Stream> list)
  5. flattenFeatureStreamToMap( Stream>>> stream)
  6. getOSIllegalCharacterStream(String path)
  7. getStream(Iterable iterable)
  8. getStringStreamFromArray(String... ids)
  9. infiniteParallelStream()