Floats Class | Guava | Java

Floats is a utility class for primitive type float. It provides Static utility methods pertaining to float primitives, that are not already found in either Float or Arrays.

Declaration :

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

Below table shows the Field summary for Guava Floats Class :




Some of the methods provided by Guava Floats 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 Floats Class :

Below given are some examples showing the implementation of methods of Guava Floats Class :
Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation
// of Guava Floats.asList() method
  
import com.google.common.primitives.Floats;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        float arr[] = { 2.6f, 4.6f, 1.2f, 2.4f, 1.5f };
  
        // Using Floats.asList() method which
        // converts array of primitives to array of objects
        List<Float> myList = Floats.asList(arr);
  
        // Displaying the elements
        System.out.println(myList);
    }
}

chevron_right


Output :

[2.6, 4.6, 1.2, 2.4, 1.5]

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation
// of Guava Floats.toArray() method
  
import com.google.common.primitives.Floats;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        List<Float> myList = Arrays.asList(2.6f, 4.6f, 1.2f, 2.4f, 1.5f);
  
        // Using Floats.toArray() method which
        // converts a List of Floats to an
        // array of float
        float[] arr = Floats.toArray(myList);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

chevron_right


Output :

[2.6, 4.6, 1.2, 2.4, 1.5]

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation
// of Guava Floats.concat() method
  
import com.google.common.primitives.Floats;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        float[] arr1 = { 2.6f, 4.6f, 1.2f };
        float[] arr2 = { 2.4f, 1.5f };
  
        // Using Floats.concat() method which
        // combines arrays from specified
        // arrays into a single array
        float[] arr = Floats.concat(arr1, arr2);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

chevron_right


Output :

[2.6, 4.6, 1.2, 2.4, 1.5]

Example 4 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation
// of Guava Floats.contains() method
  
import com.google.common.primitives.Floats;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        float[] arr = { 2.6f, 4.6f, 1.2f, 2.4f, 1.5f };
  
        // Using Floats.contains() method which
        // checks if element is present in array
        // or not
        System.out.println(Floats.contains(arr, 2.5f));
        System.out.println(Floats.contains(arr, 1.5f));
    }
}

chevron_right


output :

false
true

Example 5 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation
// of Guava Floats.min() method
  
import com.google.common.primitives.Floats;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        float[] arr = { 2.6f, 4.6f, 1.2f, 2.4f, 1.5f };
  
        // Using Floats.min() method
        System.out.println(Floats.min(arr));
    }
}

chevron_right


Output :

1.2

Example 6 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation
// of Guava Floats.max() method
  
import com.google.common.primitives.Floats;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        float[] arr = { 2.6f, 4.6f, 1.2f, 2.4f, 1.5f };
  
        // Using Floats.max() method
        System.out.println(Floats.max(arr));
    }
}

chevron_right


Output :

4.6


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.