Doubles Class | Guava | Java
Last Updated :
29 May, 2018
Doubles is a utility class for primitive type double. It provides Static utility methods pertaining to double primitives, that are not already found in either Double or Arrays.
Declaration :
@GwtCompatible(emulated=true)
public final class Doubles
extends Object
Below table shows the Field summary for Guava Doubles Class :
Some of the methods provided by Guava Doubles Class are :
Exceptions :
- min : IllegalArgumentException if array is empty.
- max : IllegalArgumentException if array is empty.
- ensureCapacity : IllegalArgumentException if minLength or padding is negative.
- toArray : NullPointerException if collection or any of its elements is null.
Below table shows some other methods provided by Guava Doubles Class :
Below given are some examples showing the implementation of Guava Doubles Class methods :
Example 1 :
import com.google.common.primitives.Doubles;
import java.util.*;
class GFG {
public static void main(String[] args)
{
double arr[] = { 2.6 , 4.6 , 1.2 , 2.4 , 1.5 };
List<Double> myList = Doubles.asList(arr);
System.out.println(myList);
}
}
|
Output :
[2.6, 4.6, 1.2, 2.4, 1.5]
Example 2 :
import com.google.common.primitives.Doubles;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Double> myList = Arrays.asList( 2.6 , 4.6 , 1.2 , 2.4 , 1.5 );
double [] arr = Doubles.toArray(myList);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[2.6, 4.6, 1.2, 2.4, 1.5]
Example 3 :
import com.google.common.primitives.Doubles;
import java.util.*;
class GFG {
public static void main(String[] args)
{
double [] arr1 = { 2.6 , 4.6 , 1.2 };
double [] arr2 = { 2.4 , 1.5 };
double [] arr = Doubles.concat(arr1, arr2);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[2.6, 4.6, 1.2, 2.4, 1.5]
Example 4 :
import com.google.common.primitives.Doubles;
class GFG {
public static void main(String[] args)
{
double [] arr = { 2.6 , 4.6 , 1.2 , 2.4 , 1.5 };
System.out.println(Doubles.contains(arr, 2.5 ));
System.out.println(Doubles.contains(arr, 1.5 ));
}
}
|
output :
false
true
Example 5 :
import com.google.common.primitives.Doubles;
class GFG {
public static void main(String[] args)
{
double [] arr = { 2.6 , 4.6 , 1.2 , 2.4 , 1.5 };
System.out.println(Doubles.min(arr));
}
}
|
Output :
1.2
Example 6 :
import com.google.common.primitives.Doubles;
class GFG {
public static void main(String[] args)
{
double [] arr = { 2.6 , 4.6 , 1.2 , 2.4 , 1.5 };
System.out.println(Doubles.max(arr));
}
}
|
Output :
4.6
Share your thoughts in the comments
Please Login to comment...