Class Floats

    • Field Summary

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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static List<Float> asList​(float... backingArray)
      Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
      static int compare​(float a, float b)
      Compares the two specified float values using Float.compare(float, float).
      static float[] concat​(float[]... arrays)
      Returns the values from each provided array combined into a single array.
      static float constrainToRange​(float value, float min, float max)
      Returns the value nearest to value which is within the closed range [min..max].
      static boolean contains​(float[] array, float target)
      Returns true if target is present as an element anywhere in array.
      static float[] ensureCapacity​(float[] 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​(float value)
      Returns a hash code for value; equal to the result of invoking ((Float) value).hashCode().
      static int indexOf​(float[] array, float target)
      Returns the index of the first appearance of the value target in array.
      static int indexOf​(float[] array, float[] 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​(float value)
      Returns true if value represents a real number.
      static String join​(String separator, float... array)
      Returns a string containing the supplied float values, converted to strings as specified by Float.toString(float), and separated by separator.
      static int lastIndexOf​(float[] array, float target)
      Returns the index of the last appearance of the value target in array.
      static Comparator<float[]> lexicographicalComparator()
      Returns a comparator that compares two float arrays lexicographically.
      static float max​(float... array)
      Returns the greatest value present in array, using the same rules of comparison as Math.max(float, float).
      static float min​(float... array)
      Returns the least value present in array, using the same rules of comparison as Math.min(float, float).
      static void reverse​(float[] array)
      Reverses the elements of array.
      static void reverse​(float[] array, int fromIndex, int toIndex)
      Reverses the elements of array between fromIndex inclusive and toIndex exclusive.
      static void sortDescending​(float[] array)
      Sorts the elements of array in descending order.
      static void sortDescending​(float[] array, int fromIndex, int toIndex)
      Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.
      static Converter<String,​Float> stringConverter()
      Returns a serializable converter object that converts between strings and floats using Float.valueOf(java.lang.String) and Float.toString().
      static float[] toArray​(Collection<? extends Number> collection)
      Returns an array containing each value of collection, converted to a float value in the manner of Number.floatValue().
      static Float tryParse​(String string)
      Parses the specified string as a single-precision floating point value.
    • Field Detail

      • BYTES

        public static final int BYTES
        The number of bytes required to represent a primitive float value.

        Java 8 users: use Float.BYTES instead.

        Since:
        10.0
        See Also:
        Constant Field Values
    • Method Detail

      • hashCode

        public static int hashCode​(float value)
        Returns a hash code for value; equal to the result of invoking ((Float) value).hashCode().

        Java 8 users: use Float.hashCode(float) instead.

        Parameters:
        value - a primitive float value
        Returns:
        a hash code for the value
      • compare

        public static int compare​(float a,
                                  float b)
        Compares the two specified float values using Float.compare(float, float). You may prefer to invoke that method directly; this method exists only for consistency with the other utilities in this package.

        Note: this method simply delegates to the JDK method Float.compare(float, float). It is provided for consistency with the other primitive types, whose compare methods were not added to the JDK until JDK 7.

        Parameters:
        a - the first float to compare
        b - the second float to compare
        Returns:
        the result of invoking Float.compare(float, float)
      • isFinite

        public static boolean isFinite​(float value)
        Returns true if value represents a real number. This is equivalent to, but not necessarily implemented as, !(Float.isInfinite(value) || Float.isNaN(value)).

        Java 8 users: use Float.isFinite(float) instead.

        Since:
        10.0
      • contains

        public static boolean contains​(float[] array,
                                       float 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 float values, possibly empty
        target - a primitive float value
        Returns:
        true if array[i] == target for some value of i
      • indexOf

        public static int indexOf​(float[] array,
                                  float 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 float values, possibly empty
        target - a primitive float value
        Returns:
        the least index i for which array[i] == target, or -1 if no such index exists.
      • indexOf

        public static int indexOf​(float[] array,
                                  float[] 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 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​(float[] array,
                                      float 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 float values, possibly empty
        target - a primitive float value
        Returns:
        the greatest index i for which array[i] == target, or -1 if no such index exists.
      • min

        @GwtIncompatible("Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.")
        public static float min​(float... array)
        Returns the least value present in array, using the same rules of comparison as Math.min(float, float).
        Parameters:
        array - a nonempty array of float values
        Returns:
        the value present in array that is less than or equal to every other value in the array
        Throws:
        IllegalArgumentException - if array is empty
      • max

        @GwtIncompatible("Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.")
        public static float max​(float... array)
        Returns the greatest value present in array, using the same rules of comparison as Math.max(float, float).
        Parameters:
        array - a nonempty array of float values
        Returns:
        the value present in array that is greater than or equal to every other value in the array
        Throws:
        IllegalArgumentException - if array is empty
      • constrainToRange

        @Beta
        public static float constrainToRange​(float value,
                                             float min,
                                             float max)
        Returns the value nearest to value which is within the closed range [min..max].

        If value is within the range [min..max], value is returned unchanged. If value is less than min, min is returned, and if value is greater than max, max is returned.

        Parameters:
        value - the float value to constrain
        min - the lower bound (inclusive) of the range to constrain value to
        max - the upper bound (inclusive) of the range to constrain value to
        Throws:
        IllegalArgumentException - if min > max
        Since:
        21.0
      • concat

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

        public static float[] ensureCapacity​(float[] 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:
        IllegalArgumentException - if minLength or padding is negative
      • join

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

        Note that Float.toString(float) formats float differently in GWT. 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 float values, possibly empty
      • sortDescending

        public static void sortDescending​(float[] array)
        Sorts the elements of array in descending order.

        Note that this method uses the total order imposed by Float.compare(float, float), which treats all NaN values as equal and 0.0 as greater than -0.0.

        Since:
        23.1
      • sortDescending

        public static void sortDescending​(float[] array,
                                          int fromIndex,
                                          int toIndex)
        Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.

        Note that this method uses the total order imposed by Float.compare(float, float), which treats all NaN values as equal and 0.0 as greater than -0.0.

        Since:
        23.1
      • reverse

        public static void reverse​(float[] array)
        Reverses the elements of array. This is equivalent to Collections.reverse(Floats.asList(array)), but is likely to be more efficient.
        Since:
        23.1
      • reverse

        public static void reverse​(float[] array,
                                   int fromIndex,
                                   int toIndex)
        Reverses the elements of array between fromIndex inclusive and toIndex exclusive. This is equivalent to Collections.reverse(Floats.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.
        Throws:
        IndexOutOfBoundsException - if fromIndex < 0, toIndex > array.length, or toIndex > fromIndex
        Since:
        23.1
      • toArray

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

        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:
        NullPointerException - if collection or any of its elements is null
        Since:
        1.0 (parameter was Collection<Float> before 12.0)
      • asList

        public static List<FloatasList​(float... 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 Float 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

        @Beta
        @GwtIncompatible
        @NullableDecl
        public static Float tryParse​(String string)
        Parses the specified string as a single-precision floating point value. The ASCII character '-' ('\u002D') is recognized as the minus sign.

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

        This implementation is likely to be faster than Float.parseFloat if many failures are expected.

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