Class Doubles


  • @GwtCompatible(emulated=true)
    public final class Doubles
    extends java.lang.Object
    Static utility methods pertaining to double primitives, that are not already found in either Double or Arrays.

    See the Guava User Guide article on primitive utilities.

    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BYTES
      The number of bytes required to represent a primitive double value.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<java.lang.Double> asList​(double... backingArray)
      Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
      static int compare​(double a, double b)
      Compares the two specified double values.
      static double[] concat​(double[]... arrays)
      Returns the values from each provided array combined into a single array.
      static boolean contains​(double[] array, double target)
      Returns true if target is present as an element anywhere in array.
      static double[] ensureCapacity​(double[] array, int minLength, int padding)
      Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.
      static int hashCode​(double value)
      Returns a hash code for value; equal to the result of invoking ((Double) value).hashCode().
      static int indexOf​(double[] array, double target)
      Returns the index of the first appearance of the value target in array.
      static int indexOf​(double[] array, double[] target)
      Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.
      static boolean isFinite​(double value)
      Returns true if value represents a real number.
      static java.lang.String join​(java.lang.String separator, double... array)
      Returns a string containing the supplied double values, converted to strings as specified by Double.toString(double), and separated by separator.
      static int lastIndexOf​(double[] array, double target)
      Returns the index of the last appearance of the value target in array.
      static java.util.Comparator<double[]> lexicographicalComparator()
      Returns a comparator that compares two double arrays lexicographically.
      static double max​(double... array)
      Returns the greatest value present in array, using the same rules of comparison as Math.max(double, double).
      static double min​(double... array)
      Returns the least value present in array, using the same rules of comparison as Math.min(double, double).
      static double[] toArray​(java.util.Collection<? extends java.lang.Number> collection)
      Returns an array containing each value of collection, converted to a double value in the manner of Number.doubleValue().
      static java.lang.Double tryParse​(java.lang.String string)
      Parses the specified string as a double-precision floating point value.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BYTES

        public static final int BYTES
        The number of bytes required to represent a primitive double value.
        Since:
        10.0
        See Also:
        Constant Field Values
    • Method Detail

      • hashCode

        public static int hashCode​(double value)
        Returns a hash code for value; equal to the result of invoking ((Double) value).hashCode().
        Parameters:
        value - a primitive double value
        Returns:
        a hash code for the value
      • compare

        public static int compare​(double a,
                                  double b)
        Compares the two specified double values. The sign of the value returned is the same as that of ((Double) a).compareTo(b). As with that method, NaN is treated as greater than all other values, and 0.0 > -0.0.
        Parameters:
        a - the first double to compare
        b - the second double to compare
        Returns:
        a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
      • isFinite

        public static boolean isFinite​(double value)
        Returns true if value represents a real number. This is equivalent to, but not necessarily implemented as, !(Double.isInfinite(value) || Double.isNaN(value)).
        Since:
        10.0
      • contains

        public static boolean contains​(double[] array,
                                       double target)
        Returns true if target is present as an element anywhere in array. Note that this always returns false when target is NaN.
        Parameters:
        array - an array of double values, possibly empty
        target - a primitive double value
        Returns:
        true if array[i] == target for some value of i
      • indexOf

        public static int indexOf​(double[] array,
                                  double target)
        Returns the index of the first appearance of the value target in array. Note that this always returns -1 when target is NaN.
        Parameters:
        array - an array of double values, possibly empty
        target - a primitive double value
        Returns:
        the least index i for which array[i] == target, or -1 if no such index exists.
      • indexOf

        public static int indexOf​(double[] array,
                                  double[] target)
        Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.

        More formally, returns the lowest index i such that java.util.Arrays.copyOfRange(array, i, i + target.length) contains exactly the same elements as target.

        Note that this always returns -1 when target contains NaN.

        Parameters:
        array - the array to search for the sequence target
        target - the array to search for as a sub-sequence of array
      • lastIndexOf

        public static int lastIndexOf​(double[] array,
                                      double target)
        Returns the index of the last appearance of the value target in array. Note that this always returns -1 when target is NaN.
        Parameters:
        array - an array of double values, possibly empty
        target - a primitive double value
        Returns:
        the greatest index i for which array[i] == target, or -1 if no such index exists.
      • min

        public static double min​(double... array)
        Returns the least value present in array, using the same rules of comparison as Math.min(double, double).
        Parameters:
        array - a nonempty array of double values
        Returns:
        the value present in array that is less than or equal to every other value in the array
        Throws:
        java.lang.IllegalArgumentException - if array is empty
      • max

        public static double max​(double... array)
        Returns the greatest value present in array, using the same rules of comparison as Math.max(double, double).
        Parameters:
        array - a nonempty array of double values
        Returns:
        the value present in array that is greater than or equal to every other value in the array
        Throws:
        java.lang.IllegalArgumentException - if array is empty
      • concat

        public static double[] concat​(double[]... arrays)
        Returns the values from each provided array combined into a single array. For example, concat(new double[] {a, b}, new double[] {}, new double[] {c} returns the array {a, b, c}.
        Parameters:
        arrays - zero or more double arrays
        Returns:
        a single array containing all the values from the source arrays, in order
      • ensureCapacity

        public static double[] ensureCapacity​(double[] array,
                                              int minLength,
                                              int padding)
        Returns an array containing the same values as array, but guaranteed to be of a specified minimum length. If array already has a length of at least minLength, it is returned directly. Otherwise, a new array of size minLength + padding is returned, containing the values of array, and zeroes in the remaining places.
        Parameters:
        array - the source array
        minLength - the minimum length the returned array must guarantee
        padding - an extra amount to "grow" the array by if growth is necessary
        Returns:
        an array containing the values of array, with guaranteed minimum length minLength
        Throws:
        java.lang.IllegalArgumentException - if minLength or padding is negative
      • join

        public static java.lang.String join​(java.lang.String separator,
                                            double... array)
        Returns a string containing the supplied double values, converted to strings as specified by Double.toString(double), and separated by separator. For example, join("-", 1.0, 2.0, 3.0) returns the string "1.0-2.0-3.0".

        Note that Double.toString(double) formats double differently in GWT sometimes. In the previous example, it returns the string "1-2-3".

        Parameters:
        separator - the text that should appear between consecutive values in the resulting string (but not at the start or end)
        array - an array of double values, possibly empty
      • lexicographicalComparator

        public static java.util.Comparator<double[]> lexicographicalComparator()
        Returns a comparator that compares two double arrays lexicographically. That is, it compares, using compare(double, double)), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example, [] < [1.0] < [1.0, 2.0] < [2.0].

        The returned comparator is inconsistent with Object.equals(Object) (since arrays support only identity equality), but it is consistent with Arrays.equals(double[], double[]).

        Since:
        2.0
        See Also:
        Lexicographical order article at Wikipedia
      • toArray

        public static double[] toArray​(java.util.Collection<? extends java.lang.Number> collection)
        Returns an array containing each value of collection, converted to a double value in the manner of Number.doubleValue().

        Elements are copied from the argument collection as if by collection.toArray(). Calling this method is as thread-safe as calling that method.

        Parameters:
        collection - a collection of Number instances
        Returns:
        an array containing the same values as collection, in the same order, converted to primitives
        Throws:
        java.lang.NullPointerException - if collection or any of its elements is null
        Since:
        1.0 (parameter was Collection<Double> before 12.0)
      • asList

        public static java.util.List<java.lang.Double> asList​(double... backingArray)
        Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). The list supports List.set(int, Object), but any attempt to set a value to null will result in a NullPointerException.

        The returned list maintains the values, but not the identities, of Double objects written to or read from it. For example, whether list.get(0) == list.get(0) is true for the returned list is unspecified.

        The returned list may have unexpected behavior if it contains NaN, or if NaN is used as a parameter to any of its methods.

        Parameters:
        backingArray - the array to back the list
        Returns:
        a list view of the array
      • tryParse

        @GwtIncompatible("regular expressions")
        @Nullable
        @Beta
        public static java.lang.Double tryParse​(java.lang.String string)
        Parses the specified string as a double-precision floating point value. The ASCII character '-' ('\u002D') is recognized as the minus sign.

        Unlike Double.parseDouble(String), this method returns null instead of throwing an exception if parsing fails. Valid inputs are exactly those accepted by Double.valueOf(String), except that leading and trailing whitespace is not permitted.

        This implementation is likely to be faster than Double.parseDouble if many failures are expected.

        Parameters:
        string - the string representation of a double value
        Returns:
        the floating point value represented by string, or null if string has a length of zero or cannot be parsed as a double value
        Since:
        14.0