Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - Doubles Class
Doubles is a utility class for primitive type double.
Class Declaration
Following is the declaration for com.google.common.primitives.Doubles class −
@GwtCompatible(emulated = true)
public final class Doubles
extends Object
| Sr.No | Field & Description |
|---|---|
| 1 |
static int BYTES The number of bytes required to represent a primitive double value. |
| Sr.No | Method & Description |
|---|---|
| 1 |
static List<Double> asList(double... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 |
static int compare(double a, double b) Compares the two specified double values. |
| 3 |
static double[] concat(double[]... arrays) Returns the values from each provided array combined into a single array. |
| 4 |
static boolean contains(double[] array, double target) Returns true if target is present as an element anywhere in array. |
| 5 |
static double[] ensureCapacity(double[] array, int minLength, int padding) Returns an array containing the same values as the array, but guaranteed to be of a specified minimum length. |
| 6 |
static int hashCode(double value) Returns a hash code for value; equal to the result of invoking ((Double) value).hashCode(). |
| 7 |
static int indexOf(double[] array, double target) Returns the index of the first appearance of the value target in array. |
| 8 |
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. |
| 9 |
static boolean isFinite(double value) Returns true if value represents a real number. |
| 10 |
static String join(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. |
| 11 |
static int lastIndexOf(double[] array, double target) Returns the index of the last appearance of the value target in array. |
| 12 |
static Comparator<double[]> lexicographicalComparator() Returns a comparator that compares two double arrays lexicographically. |
| 13 |
static double max(double... array) Returns the greatest value present in array, using the same rules of comparison as Math.max(double, double). |
| 14 |
static double min(double... array) Returns the least value present in array, using the same rules of comparison as Math.min(double, double). |
| 15 |
static Converter<String,Double> stringConverter() Returns a serializable converter object that converts between strings and doubles using Double.valueOf(java.lang.String) and Double.toString(). |
| 16 |
static double[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a double value in the manner of Number.doubleValue(). |
| 17 |
static Double tryParse(String string) Parses the specified string as a double-precision floating point value. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example - Converting Array of Doubles to List and vice versa
GuavaTester.java
package com.tutorialspoint;
import java.util.List;
import com.google.common.primitives.Doubles;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testDoubles();
}
private void testDoubles() {
double[] doubleArray = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
//convert array of primitives to array of objects
List<Double> objectArray = Doubles.asList(doubleArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
doubleArray = Doubles.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< doubleArray.length ; i++) {
System.out.print(doubleArray[i] + " ");
}
System.out.println("]");
}
}
Output
Run the GuavaTester and verify the result −
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] [ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ]
Example - Checking Presence of Element in Array of Doubles
GuavaTester.java
package com.tutorialspoint;
import com.google.common.primitives.Doubles;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testDoubles();
}
private void testDoubles() {
double[] doubleArray = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
//check if element is present in the list of primitives or not
System.out.println("5.0 is in list? " + Doubles.contains(doubleArray, 5.0f));
//return the index of element
System.out.println("5.0 position in list " + Doubles.indexOf(doubleArray, 5.0f));
}
}
Output
Run the GuavaTester and verify the result −
5.0 is in list? true 5.0 position in list 4
Example - Getting Minimum/Maximum from an Array of Doubles
GuavaTester.java
package com.tutorialspoint;
import java.util.List;
import com.google.common.primitives.Doubles;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testDoubles();
}
private void testDoubles() {
double[] doubleArray = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
//Returns the minimum
System.out.println("Min: " + Doubles.min(doubleArray));
//Returns the maximum
System.out.println("Max: " + Doubles.max(doubleArray));
}
}
Output
Run the GuavaTester and verify the result −
Min: 1.0 Max: 9.0