Guava – Floats Class

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

Floats is a utility class for primitive type float.

Class Declaration

Following is the declaration for com.google.common.primitives.Floats class −

@GwtCompatible(emulated = true)
   public final class Floats
      extends Object

Fields

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

Methods

Sr.NoMethod & Description
1static List<Float> asList(float… backingArray)Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(float a, float b)Compares the two specified float values using Float.compare(float, float).
3static float[] concat(float[]… arrays)Returns the values from each provided array combined into a single array.
4static boolean contains(float[] array, float target)Returns true if target is present as an element anywhere in array.
5static float[] ensureCapacity(float[] 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(float value)Returns a hash code for value; equal to the result of invoking ((Float) value).hashCode().
7static int indexOf(float[] array, float target)Returns the index of the first appearance of the value target in array.
8static int indexOf(float[] array, float[] 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(float value)Returns true if value represents a real number.
10static String join(String separator, float… array)Returns a string containing the supplied float values, converted to strings as specified by Float.toString(float), and separated by separator.
11static int lastIndexOf(float[] array, float target)Returns the index of the last appearance of the value target in array.
12static Comparator<float[]> lexicographicalComparator()Returns a comparator that compares two float arrays lexicographically.
13static float max(float… array)Returns the greatest value present in array, using the same rules of comparison as Math.min(float, float).
14static float min(float… array)Returns the least value present in array, using the same rules of comparison as Math.min(float, float).
15static Converter<String,Float> stringConverter()Returns a serializable converter object that converts between strings and floats using Float.valueOf(java.lang.String) and Float.toString().
16static float[] toArray(Collection<? extends Number> collection)Returns an array containing each value of collection, converted to a float value in the manner of Number.floatValue().
17static Float tryParse(String string)Parses the specified string as a single-precision floating point value.

Methods Inherited

This class inherits methods from the following class −

  • java.lang.Object

Example of Floats 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.Floats;

public class GuavaTester {

   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testFloats();
   }

   private void testFloats() {
      float[] floatArray = {1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f};

      //convert array of primitives to array of objects
      List<Float> objectArray = Floats.asList(floatArray);
      System.out.println(objectArray.toString());

      //convert array of objects to array of primitives
      floatArray = Floats.toArray(objectArray);
      System.out.print("[ ");
      
      for(int i = 0; i< floatArray.length ; i++) {
         System.out.print(floatArray[i] + " ");
      }
      
      System.out.println("]");
      
      //check if element is present in the list of primitives or not
      System.out.println("5.0 is in list? " + Floats.contains(floatArray, 5.0f));

      //return the index of element
      System.out.println("5.0 position in list " + Floats.indexOf(floatArray, 5.0f));

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

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

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