Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Guava | Floats.min() method with Examples

Java Guava | Floats.min() method with Examples

Floats.min() is a method of Floats Class in Guava library which is used to find the least value present in an array. The value returned by this method is the smallest float value in the specified array.

Syntax:

public static float min(float... array)

Parameters: This method takes a mandatory parameter array which is a nonempty array of float values.

Return Value: This method returns a float value that is the minimum value in the specified array.

Exceptions: The method throws IllegalArgumentException if the array is empty.

Below programs illustrate the use of the above method:

Example 1 :




// Java code to show implementation of
// Guava's Floats.min() method
  
import com.google.common.primitives.Floats;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a float array
        float[] arr = { 2.2f, 4.3f, 6.4f, 10.5f,
                        0.6f, -5.7f, 15.8f };
  
        // Using Floats.min() method to get the
        // minimum value present in the array
        System.out.println("Minimum Value is : "
                           + Floats.min(arr));
    }
}


Output:

Minimum Value is : -5.7

Example 2:




// Java code to show implementation of
// Guava's Floats.min() method
  
import com.google.common.primitives.Floats;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a float array
        float[] arr = {};
  
        try {
            // Using Floats.min() method to get the
            // minimum value present in the array
            // This should raise "IllegalArgumentException"
            // as the array is empty
            System.out.println("Minimum Value is : "
                               + Floats.min(arr));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.IllegalArgumentException

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Floats.html#min(float…)

RELATED ARTICLES

Most Popular

Recent Comments