Friday, October 10, 2025
HomeLanguagesJavaLinkedHashSet contains() Method in Java with Examples

LinkedHashSet contains() Method in Java with Examples

In Java, LinkedHashSet class contains methods known as contains() which is used to return true if this set contains the specified element otherwise false.

Syntax: 

public boolean contains(Object o)

Parameters: Element o as a parameter whose presence in this set is to be tested.

Return Type: A boolean value, true if this set contains the specified element.

Example 1:

Java




// Java program to Illustrate contains() Method
// of LinkedHashSet class
// For String value
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty LinkedHashSet
            // Declaring object of string type
            LinkedHashSet<String> linkset
                = new LinkedHashSet<String>();
 
            // Populating above HashSet
            linkset.add("A");
            linkset.add("B");
            linkset.add("C");
 
            // Printing all elements of above LinkedHashSet
            System.out.println("LinkedHashSet: " + linkset);
 
            // Checking the existence of element
            // using contains() method
            boolean exist = linkset.contains("C");
 
            // Printing boolean value if present or not
            System.out.println("Is the element"
                               + " 'C' present: " + exist);
        }
 
        // Catch block to check for exceptions
        catch (NullPointerException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

LinkedHashSet: [A, B, C]
Is the element 'C' present: true

 

Example 2:

Java




// Java program to Illustrate contains() Method
// of LinkedHashSet class
// For Integer value
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty LinkedHashSet
            // Declaring object of integer type
            LinkedHashSet<Integer> linkset
                = new LinkedHashSet<Integer>();
 
            // Populating above HashSet
            linkset.add(10);
            linkset.add(20);
            linkset.add(30);
 
            // Printing all elements of above LinkedHashSet
            System.out.println("LinkedHashSet: " + linkset);
 
            // Checking the existence of element
            // using contains() method
            boolean exist = linkset.contains(25);
 
            // Printing boolean value if present or not
            System.out.println("Is the element"
                               + " '25' present: " + exist);
        }
 
        // Catch block to check for exceptions
        catch (NullPointerException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

LinkedHashSet: [10, 20, 30]
Is the element '25' present: false

 

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS