Class Doubles
double primitives, that are not already found in
either Double or Arrays.
See the Guava User Guide article on primitive utilities.
- Since:
- 1.0
- Author:
- Kevin Bourrillion
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intThe number of bytes required to represent a primitivedoublevalue.
- 
Method SummaryModifier and TypeMethodDescriptionasList(double... backingArray) Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[]).static intcompare(double a, double b) Compares the two specifieddoublevalues.static double[]concat(double[]... arrays) Returns the values from each provided array combined into a single array.static doubleconstrainToRange(double value, double min, double max) Returns the value nearest tovaluewhich is within the closed range[min..max].static booleancontains(double[] array, double target) Returnstrueiftargetis present as an element anywhere inarray.static double[]ensureCapacity(double[] array, int minLength, int padding) Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length.static inthashCode(double value) Returns a hash code forvalue; obsolete alternative toDouble.hashCode(double).static intindexOf(double[] array, double target) Returns the index of the first appearance of the valuetargetinarray.static intindexOf(double[] array, double[] target) Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.static booleanisFinite(double value) Returnstrueifvaluerepresents a real number.static StringReturns a string containing the supplieddoublevalues, converted to strings as specified byDouble.toString(double), and separated byseparator.static intlastIndexOf(double[] array, double target) Returns the index of the last appearance of the valuetargetinarray.static Comparator<double[]> Returns a comparator that compares twodoublearrays lexicographically.static doublemax(double... array) Returns the greatest value present inarray, using the same rules of comparison asMath.max(double, double).static doublemin(double... array) Returns the least value present inarray, using the same rules of comparison asMath.min(double, double).static voidreverse(double[] array) Reverses the elements ofarray.static voidreverse(double[] array, int fromIndex, int toIndex) Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive.static voidrotate(double[] 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(double[] array, int distance, int fromIndex, int toIndex) Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive.static voidsortDescending(double[] array) Sorts the elements ofarrayin descending order.static voidsortDescending(double[] array, int fromIndex, int toIndex) Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.Returns a serializable converter object that converts between strings and doubles usingDouble.valueOf(String)andDouble.toString().static double[]toArray(Collection<? extends Number> collection) Returns an array containing each value ofcollection, converted to adoublevalue in the manner ofNumber.doubleValue().Parses the specified string as a double-precision floating point value.
- 
Field Details- 
BYTESpublic static final int BYTESThe number of bytes required to represent a primitivedoublevalue.Prefer Double.BYTESinstead.- Since:
- 10.0
- See Also:
 
 
- 
- 
Method Details- 
hashCodeReturns a hash code forvalue; obsolete alternative toDouble.hashCode(double).- Parameters:
- value- a primitive- doublevalue
- Returns:
- a hash code for the value
 
- 
compareCompares the two specifieddoublevalues. The sign of the value returned is the same as that of((Double) a).compareTo(b). As with that method,NaNis treated as greater than all other values, and0.0 > -0.0.Note: this method simply delegates to the JDK method Double.compare(double, double). 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- doubleto compare
- b- the second- doubleto compare
- Returns:
- a negative value if ais less thanb; a positive value ifais greater thanb; or zero if they are equal
 
- 
isFiniteReturnstrueifvaluerepresents a real number. This is equivalent to, but not necessarily implemented as,!(Double.isInfinite(value) || Double.isNaN(value)).Prefer Double.isFinite(double)instead.- Since:
- 10.0
 
- 
containspublic static boolean contains(double[] array, double target) Returnstrueiftargetis present as an element anywhere inarray. Note that this always returnsfalsewhentargetisNaN.- Parameters:
- array- an array of- doublevalues, possibly empty
- target- a primitive- doublevalue
- Returns:
- trueif- array[i] == targetfor some value of- i
 
- 
indexOfpublic static int indexOf(double[] array, double target) Returns the index of the first appearance of the valuetargetinarray. Note that this always returns-1whentargetisNaN.- Parameters:
- array- an array of- doublevalues, possibly empty
- target- a primitive- doublevalue
- Returns:
- the least index ifor whicharray[i] == target, or-1if no such index exists.
 
- 
indexOfpublic static int indexOf(double[] array, double[] 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 sequence- target
- target- the array to search for as a sub-sequence of- array
 
- 
lastIndexOfpublic static int lastIndexOf(double[] array, double target) Returns the index of the last appearance of the valuetargetinarray. Note that this always returns-1whentargetisNaN.- Parameters:
- array- an array of- doublevalues, possibly empty
- target- a primitive- doublevalue
- Returns:
- the greatest index ifor whicharray[i] == target, or-1if no such index exists.
 
- 
min@GwtIncompatible("Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.") public static double min(double... array) Returns the least value present inarray, using the same rules of comparison asMath.min(double, double).- Parameters:
- array- a nonempty array of- doublevalues
- Returns:
- the value present in arraythat is less than or equal to every other value in the array
- Throws:
- IllegalArgumentException- if- arrayis empty
 
- 
max@GwtIncompatible("Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.") public static double max(double... array) Returns the greatest value present inarray, using the same rules of comparison asMath.max(double, double).- Parameters:
- array- a nonempty array of- doublevalues
- Returns:
- the value present in arraythat is greater than or equal to every other value in the array
- Throws:
- IllegalArgumentException- if- arrayis empty
 
- 
constrainToRangepublic static double constrainToRange(double value, double min, double 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.Java 21+ users: Use Math.clampinstead.- Parameters:
- value- the- doublevalue to constrain
- min- the lower bound (inclusive) of the range to constrain- valueto
- max- the upper bound (inclusive) of the range to constrain- valueto
- Throws:
- IllegalArgumentException- if- min > max
- Since:
- 21.0
 
- 
concatpublic 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- doublearrays
- Returns:
- a single array containing all the values from the source arrays, in order
- Throws:
- IllegalArgumentException- if the total number of elements in- arraysdoes not fit in an- int
 
- 
stringConverterReturns a serializable converter object that converts between strings and doubles usingDouble.valueOf(String)andDouble.toString().- Since:
- 16.0
 
- 
ensureCapacitypublic static double[] ensureCapacity(double[] 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 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 lengthminLength
- Throws:
- IllegalArgumentException- if- minLengthor- paddingis negative
 
- 
joinReturns a string containing the supplieddoublevalues, converted to strings as specified byDouble.toString(double), and separated byseparator. For example,join("-", 1.0, 2.0, 3.0)returns the string"1.0-2.0-3.0".Note that Double.toString(double)formatsdoubledifferently 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- doublevalues, possibly empty
 
- 
lexicographicalComparatorReturns a comparator that compares twodoublearrays lexicographically. That is, it compares, usingcompare(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 withArrays.equals(double[], double[]).- Since:
- 2.0
 
- 
sortDescendingpublic static void sortDescending(double[] array) Sorts the elements ofarrayin descending order.Note that this method uses the total order imposed by Double.compare(double, double), which treats all NaN values as equal and 0.0 as greater than -0.0.- Since:
- 23.1
 
- 
sortDescendingpublic static void sortDescending(double[] array, int fromIndex, int toIndex) Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.Note that this method uses the total order imposed by Double.compare(double, double), which treats all NaN values as equal and 0.0 as greater than -0.0.- Since:
- 23.1
 
- 
reversepublic static void reverse(double[] array) Reverses the elements ofarray. This is equivalent toCollections.reverse(Doubles.asList(array)), but is likely to be more efficient.- Since:
- 23.1
 
- 
reversepublic static void reverse(double[] array, int fromIndex, int toIndex) Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.reverse(Doubles.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
 
- 
rotatepublic static void rotate(double[] 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(Bytes.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
 
- 
rotatepublic static void rotate(double[] array, int distance, int fromIndex, int toIndex) Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.rotate(Bytes.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- if- fromIndex < 0,- toIndex > array.length, or- toIndex > fromIndex
- Since:
- 32.0.0
 
- 
toArrayReturns an array containing each value ofcollection, converted to adoublevalue in the manner ofNumber.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- Numberinstances
- Returns:
- an array containing the same values as collection, in the same order, converted to primitives
- Throws:
- NullPointerException- if- collectionor any of its elements is null
- Since:
- 1.0 (parameter was Collection<Double>before 12.0)
 
- 
asListReturns 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 Doubleobjects 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. Note: when possible, you should represent your data as an ImmutableDoubleArrayinstead, which has anasListview.- Parameters:
- backingArray- the array to back the list
- Returns:
- a list view of the array
 
- 
tryParseParses 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 returnsnullinstead of throwing an exception if parsing fails. Valid inputs are exactly those accepted byDouble.valueOf(String), except that leading and trailing whitespace is not permitted.This implementation is likely to be faster than Double.parseDoubleif many failures are expected.- Parameters:
- string- the string representation of a- doublevalue
- Returns:
- the floating point value represented by string, ornullifstringhas a length of zero or cannot be parsed as adoublevalue
- Throws:
- NullPointerException- if- stringis- null
- Since:
- 14.0
 
 
-