Guava – Doubles Class

  • Post author:
  • Post category:Guava
  • Post comments:0 Comments
Doubles

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

Fields

Sr.NoField & Description
1static int BYTESThe number of bytes required to represent a primitive double value.

Methods

Sr.NoMethod & Description
1static List<Double> asList(double… backingArray)Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(double a, double b)Compares the two specified double values.
3static double[] concat(double[]… arrays)Returns the values from each provided array combined into a single array.
4static boolean contains(double[] array, double target)Returns true if target is present as an element anywhere in array.
5static 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.
6static int hashCode(double value)Returns a hash code for value; equal to the result of invoking ((Double) value).hashCode().
7static int indexOf(double[] array, double target)Returns the index of the first appearance of the value target in array.
8static 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.
9static boolean isFinite(double value)Returns true if value represents a real number.
10static 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.
11static int lastIndexOf(double[] array, double target)Returns the index of the last appearance of the value target in array.
12static Comparator<double[]> lexicographicalComparator()Returns a comparator that compares two double arrays lexicographically.
13static double max(double… array)Returns the greatest value present in array, using the same rules of comparison as Math.max(double, double).
14static double min(double… array)Returns the least value present in array, using the same rules of comparison as Math.min(double, double).
15static Converter<String,Double> stringConverter()Returns a serializable converter object that converts between strings and doubles using Double.valueOf(java.lang.String) and Double.toString().
16static 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().
17static 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 of Doubles Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

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("]");
      
      //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));

      //Returns the minimum		
      System.out.println("Min: " + Doubles.min(doubleArray));

      //Returns the maximum		
      System.out.println("Max: " + Doubles.max(doubleArray));	
   }
}

Verify the Result

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See 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 ]
5.0 is in list? true
5.0 position in list 4
Min: 1.0
Max: 9.0

Previous page:-Click Here

Leave a Reply