Thursday, October 2, 2025
HomeLanguagesJavaJava Guava | Longs.contains() method with Examples

Java Guava | Longs.contains() method with Examples

The Longs.contains() method of Guava’s Longs Class is used to check if a value i.e. target is present in the array or not. This target value and the array are taken as parameters to this method. And this method returns a boolean value that states whether the target value is

Syntax:

public static boolean contains(long[] array, 
                               long target)

Parameters: This method accepts two parameters:

  • array: which is the array of values in which the target value is to be searched
  • target: which is the long value to be checked for presence.

Return Value: The method returns True if for some value of i, the value present at ith index is equal to the target, else returns False.

Exceptions: The method doesn’t throw any exception.

Example 1:




// Java code to show implementation of
// Guava's Longs.contains() 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 = { 5L, 4L, 3L, 2L, 1L };
  
        long target = 3L;
  
        // Using Longs.contains() method to search
        // for an element in the array. The method
        // returns true if element is found, else
        // returns false
        if (Longs.contains(arr, target))
            System.out.println("Target is present"
                               + " in the array");
        else
            System.out.println("Target is not "
                               + "present in the array");
    }
}


Output:

Target is present in the array

Example 2:




// Java code to show implementation of
// Guava's Longs.contains() 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 = { 2L, 4L, 6L, 8L, 10L };
  
        long target = 7L;
  
        // Using Longs.contains() method to search
        // for an element in the array. The method
        // returns true if element is found, else
        // returns false
        if (Longs.contains(arr, target))
            System.out.println("Target is present"
                               + " in the array");
        else
            System.out.println("Target is not "
                               + "present in the array");
    }
}


Output:

Target is not present in the array

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

RELATED ARTICLES

Most Popular

Dominic
32330 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6817 POSTS0 COMMENTS
Ted Musemwa
7078 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6774 POSTS0 COMMENTS