Saturday, September 27, 2025
HomeLanguagesJavaJava Guava | Longs.max() method with Examples

Java Guava | Longs.max() method with Examples

Longs.max() is a method of Longs Class in Guava library which is used to find the greatest value present in an array. The value returned by this method is the largest long value in the specified array.

Syntax:

public static long max(long... array)

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

Return Value: This method returns a long value that is the maximum 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 Longs.max() method
  
import com.google.common.primitives.Longs;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a long array
        long[] arr = { 2, 4, 6, 10, 0,
                       -5, 15, 7 };
  
        // Using Longs.max() method to get the
        // maximum value present in the array
        System.out.println("Maximum value is : "
                           + Longs.max(arr));
    }
}


Output:

Maximum value is : 15

Example 2 :




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


Output:

java.lang.IllegalArgumentException

Reference: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#max-long…-

RELATED ARTICLES

Most Popular

Dominic
32323 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11914 POSTS0 COMMENTS
Shaida Kate Naidoo
6807 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS