Java BigInteger Max max(BigInteger a, BigInteger b)

Here you can find the source of max(BigInteger a, BigInteger b)

Description

Since Math.max() doesn't handle BigInteger

License

Open Source License

Parameter

Parameter Description
a Can be null
b Can be null

Return

the max of a,b, or null if both are null

Declaration

public static BigInteger max(BigInteger a, BigInteger b) 

Method Source Code

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

import java.math.BigInteger;

import java.util.Collection;

public class Main {
    /**/*from ww w  .j  a  va  2  s.co  m*/
     * 
     * @param xs
     *            Can contain nulls -- which are ignored
     * @return this will be a member of the xs, null if the xs had no non-null
     *         values
     */
    public static <N extends Number> N max(Collection<N> xs) {
        assert !xs.isEmpty();
        double max = Double.NEGATIVE_INFINITY;
        N maxn = null;
        for (N number : xs) {
            // skip null!
            if (number == null) {
                continue;
            }
            // Cast down into double?! ??use lessThan instead??
            double x = number.doubleValue();
            if (x > max || maxn == null) {
                max = x;
                maxn = number;
            }
        }
        return maxn;
    }

    /**
     * 
     * @param values
     *            Must not be zero-length or null. Cannot contain nulls (because: double), but see
     *            {@link #max(Collection)} which can.
     * @return max of values
     */
    public static double max(double... values) {
        double max = values[0];
        for (double i : values) {
            if (i > max) {
                max = i;
            }
        }
        return max;
    }

    public static int max(int... values) {
        int max = values[0];
        for (int i : values) {
            if (i > max) {
                max = i;
            }
        }
        return max;
    }

    /**
     * Since Math.max() doesn't handle BigInteger
     * 
     * @param a Can be null
     * @param b Can be null
     * @return the max of a,b, or null if both are null
     */
    public static BigInteger max(BigInteger a, BigInteger b) {
        if (a == null)
            return b;
        if (b == null)
            return a;
        int c = a.compareTo(b);
        return c < 0 ? b : a;
    }
}

Related

  1. max(BigInteger first, BigInteger second)
  2. maxValue(final BigInteger... values)