float primitives, that are not already found in
either Float or Arrays.
See the Guava User Guide article on primitive utilities.
- Since:
- 1.0
- Author:
- Kevin Bourrillion
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe number of bytes required to represent a primitivefloatvalue. -
Method Summary
Modifier and TypeMethodDescriptionasList(float... backingArray) Returns a fixed-size list backed by the specified array, similar toArrays#asList(Object[]).static intcompare(float a, float b) Compares the two specifiedfloatvalues usingFloat#compare(float, float).static float[]concat(float[]... arrays) Returns the values from each provided array combined into a single array.static floatconstrainToRange(float value, float min, float max) Returns the value nearest tovaluewhich is within the closed range[min..max].static booleancontains(float[] array, float target) Returnstrueiftargetis present as an element anywhere inarray.static float[]ensureCapacity(float[] array, int minLength, int padding) Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length.static inthashCode(float value) Returns a hash code forvalue; equal to the result of invoking((Float) value).hashCode().static intindexOf(float[] array, float target) Returns the index of the first appearance of the valuetargetinarray.static intindexOf(float[] array, float[] target) Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.static booleanisFinite(float value) Returnstrueifvaluerepresents a real number.static StringReturns a string containing the suppliedfloatvalues, converted to strings as specified byFloat#toString(float), and separated byseparator.static intlastIndexOf(float[] array, float target) Returns the index of the last appearance of the valuetargetinarray.static Comparator<float[]>Returns a comparator that compares twofloatarrays lexicographically.static floatmax(float... array) Returns the greatest value present inarray, using the same rules of comparison asMath#max(float, float).static floatmin(float... array) Returns the least value present inarray, using the same rules of comparison asMath#min(float, float).static voidreverse(float[] array) Reverses the elements ofarray.static voidreverse(float[] array, int fromIndex, int toIndex) Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive.static voidrotate(float[] array, int distance) Performs a right rotation ofarrayof "distance" places, so that the first element is moved to index "distance", and the element at indexiends up at index(distance + i) mod array.length.static voidrotate(float[] array, int distance, int fromIndex, int toIndex) Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive.static voidsortDescending(float[] array) Sorts the elements ofarrayin descending order.static voidsortDescending(float[] array, int fromIndex, int toIndex) Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.Returns a serializable converter object that converts between strings and floats usingFloat#valueOfandFloat#toString().static float[]toArray(Collection<? extends Number> collection) Returns an array containing each value ofcollection, converted to afloatvalue in the manner ofNumber#floatValue.static FloatParses the specified string as a single-precision floating point value.
-
Field Details
-
BYTES
public static final int BYTESThe number of bytes required to represent a primitivefloatvalue.Java 8 users: use
Float#BYTESinstead.- Since:
- 10.0
- See Also:
-
-
Method Details
-
hashCode
public static int hashCode(float value) Returns a hash code forvalue; equal to the result of invoking((Float) value).hashCode().Java 8 users: use
Float#hashCode(float)instead.- Parameters:
value- a primitivefloatvalue- Returns:
- a hash code for the value
-
compare
public static int compare(float a, float b) Compares the two specifiedfloatvalues usingFloat#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. 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 firstfloatto compareb- the secondfloatto compare- Returns:
- the result of invoking
Float#compare(float, float)
-
isFinite
public static boolean isFinite(float value) Returnstrueifvaluerepresents 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) Returnstrueiftargetis present as an element anywhere inarray. Note that this always returnsfalsewhentargetisNaN.- Parameters:
array- an array offloatvalues, possibly emptytarget- a primitivefloatvalue- Returns:
trueifarray[i] == targetfor some value ofi
-
indexOf
public static int indexOf(float[] array, float target) Returns the index of the first appearance of the valuetargetinarray. Note that this always returns-1whentargetisNaN.- Parameters:
array- an array offloatvalues, possibly emptytarget- a primitivefloatvalue- Returns:
- the least index
ifor whicharray[i] == target, or-1if no such index exists.
-
indexOf
public static int indexOf(float[] array, float[] target) Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.More formally, returns the lowest index
isuch thatArrays.copyOfRange(array, i, i + target.length)contains exactly the same elements astarget.Note that this always returns
-1whentargetcontainsNaN.- Parameters:
array- the array to search for the sequencetargettarget- the array to search for as a sub-sequence ofarray
-
lastIndexOf
public static int lastIndexOf(float[] array, float target) Returns the index of the last appearance of the valuetargetinarray. Note that this always returns-1whentargetisNaN.- Parameters:
array- an array offloatvalues, possibly emptytarget- a primitivefloatvalue- Returns:
- the greatest index
ifor whicharray[i] == target, or-1if no such index exists.
-
min
public static float min(float... array) Returns the least value present inarray, using the same rules of comparison asMath#min(float, float).- Parameters:
array- a nonempty array offloatvalues- Returns:
- the value present in
arraythat is less than or equal to every other value in the array - Throws:
IllegalArgumentException- ifarrayis empty
-
max
public static float max(float... array) Returns the greatest value present inarray, using the same rules of comparison asMath#max(float, float).- Parameters:
array- a nonempty array offloatvalues- Returns:
- the value present in
arraythat is greater than or equal to every other value in the array - Throws:
IllegalArgumentException- ifarrayis empty
-
constrainToRange
public static float constrainToRange(float value, float min, float max) Returns the value nearest tovaluewhich is within the closed range[min..max].If
valueis within the range[min..max],valueis returned unchanged. Ifvalueis less thanmin,minis returned, and ifvalueis greater thanmax,maxis returned.- Parameters:
value- thefloatvalue to constrainmin- the lower bound (inclusive) of the range to constrainvaluetomax- the upper bound (inclusive) of the range to constrainvalueto- Throws:
IllegalArgumentException- ifmin > 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 morefloatarrays- Returns:
- a single array containing all the values from the source arrays, in order
-
stringConverter
Returns a serializable converter object that converts between strings and floats usingFloat#valueOfandFloat#toString().- Since:
- 16.0
-
ensureCapacity
public static float[] ensureCapacity(float[] array, int minLength, int padding) Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length. Ifarrayalready has a length of at leastminLength, it is returned directly. Otherwise, a new array of sizeminLength + paddingis returned, containing the values ofarray, and zeroes in the remaining places.- Parameters:
array- the source arrayminLength- the minimum length the returned array must guaranteepadding- an extra amount to "grow" the array by if growth is necessary- Returns:
- an array containing the values of
array, with guaranteed minimum lengthminLength - Throws:
IllegalArgumentException- ifminLengthorpaddingis negative
-
join
Returns a string containing the suppliedfloatvalues, converted to strings as specified byFloat#toString(float), and separated byseparator. For example,join("-", 1.0f, 2.0f, 3.0f)returns the string"1.0-2.0-3.0".Note that
Float#toString(float)formatsfloatdifferently 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 offloatvalues, possibly empty
-
lexicographicalComparator
Returns a comparator that compares twofloatarrays lexicographically. That is, it compares, using#compare(float, float)), 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.0f] < [1.0f, 2.0f] < [2.0f].The returned comparator is inconsistent with
Object#equals(Object)(since arrays support only identity equality), but it is consistent withArrays#equals(float[], float[]).- Since:
- 2.0
-
sortDescending
public static void sortDescending(float[] array) Sorts the elements ofarrayin descending order.Note that this method uses the total order imposed by
Float#compare, 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 ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.Note that this method uses the total order imposed by
Float#compare, 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 ofarray. This is equivalent toCollections.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 ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.reverse(Floats.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.- Throws:
IndexOutOfBoundsException- iffromIndex < 0,toIndex > array.length, ortoIndex > fromIndex- Since:
- 23.1
-
rotate
public static void rotate(float[] array, int distance) Performs a right rotation ofarrayof "distance" places, so that the first element is moved to index "distance", and the element at indexiends up at index(distance + i) mod array.length. This is equivalent toCollections.rotate(Floats.asList(array), distance), but is considerably faster and avoids allocation and garbage collection.The provided "distance" may be negative, which will rotate left.
- Since:
- 32.0.0
-
rotate
public static void rotate(float[] array, int distance, int fromIndex, int toIndex) Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.rotate(Floats.asList(array).subList(fromIndex, toIndex), distance), but is considerably faster and avoids allocations and garbage collection.The provided "distance" may be negative, which will rotate left.
- Throws:
IndexOutOfBoundsException- iffromIndex < 0,toIndex > array.length, ortoIndex > fromIndex- Since:
- 32.0.0
-
toArray
Returns an array containing each value ofcollection, converted to afloatvalue in the manner ofNumber#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 ofNumberinstances- Returns:
- an array containing the same values as
collection, in the same order, converted to primitives - Throws:
NullPointerException- ifcollectionor any of its elements is null- Since:
- 1.0 (parameter was
Collection<Float>before 12.0)
-
asList
Returns a fixed-size list backed by the specified array, similar toArrays#asList(Object[]). The list supportsList#set(int, Object), but any attempt to set a value tonullwill result in aNullPointerException.The returned list maintains the values, but not the identities, of
Floatobjects written to or read from it. For example, whetherlist.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 ifNaNis used as a parameter to any of its methods.The returned list is serializable.
- Parameters:
backingArray- the array to back the list- Returns:
- a list view of the array
-
tryParse
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 returnsnullinstead of throwing an exception if parsing fails. Valid inputs are exactly those accepted byFloat#valueOf(String), except that leading and trailing whitespace is not permitted.This implementation is likely to be faster than
Float.parseFloatif many failures are expected.- Parameters:
string- the string representation of afloatvalue- Returns:
- the floating point value represented by
string, ornullifstringhas a length of zero or cannot be parsed as afloatvalue - Throws:
NullPointerException- ifstringisnull- Since:
- 14.0
-