Open In App

LinkedHashSet contains() Method in Java with Examples

Last Updated : 18 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

 



Similar Reads

LinkedHashSet contains() method in Java
The Java.util.LinkedHashSet.contains() method is used to check whether a specific element is present in the LinkedHashSet or not. So basically it is used to check if a Set contains any particular element. Syntax: Hash_Set.contains(Object element) Parameters: The parameter element is of the type of LinkedHashSet. This is the element that needs to be
2 min read
LinkedHashSet clear() method in Java with Examples
The clear() method of java.util.LinkedHashSet class is used to remove all of the elements from this set. The set will be empty after this call returns. Syntax: public void clear() Return Value: This method does not return anything. Below are the examples to illustrate the clear() method. Example 1: // Java program to demonstrate // clear() method /
2 min read
LinkedHashSet add() method in Java with Examples
The add() method in Java LinkedHashSet is used to add a specific element into a LinkedHashSet. This method will add the element only if the specified element is not present in the LinkedHashSet else the function will return False if the element is already present in the LinkedHashSet. Syntax: Hash_Set.add(Object element) Parameters: The parameter e
1 min read
LinkedHashSet clone() Method in Java with Examples
The clone() method of LinkedHashSet class is used to return a shallow copy of the mentioned hash set. It just creates a copy of the set. --&gt; java.util Package --&gt; LinkedHashSet Class --&gt; clone() Method Syntax: linked_hash_set.clone() Parameters: The method does not take any parameters. Return Type: The method just returns a copy of the Lin
1 min read
LinkedHashSet in Java with Examples
The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cyc
9 min read
LinkedHashSet removeAll() method in Java with Example
The removeAll() method of java.util.LinkedHashSet class is used to remove from this set all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from this set. Returns Value: This method returns true
3 min read
LinkedHashSet toArray(T[]) method in Java with Example
The toArray(T[]) method method of LinkedHashSet class in Java is used to form an array of the same elements as that of the LinkedHashSet. It returns an array containing all of the elements in this LinkedHashSet in the correct order; the run-time type of the returned array is that of the specified array. If the LinkedHashSet fits in the specified ar
4 min read
LinkedHashSet toArray() method in Java with Example
The toArray() method of Java LinkedHashSet is used to form an array of the same elements as that of the LinkedHashSet. Basically, it copies all the element from a LinkedHashSet to a new array. Syntax: Object[] arr = LinkedHashSet.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the
2 min read
LinkedHashSet equals() method in Java with Example
The equals() method of java.util.LinkedHashSet class is used to compare the specified object with this set for equality. Returns true if and only if the specified object is also a set, both sets have the same size, and all corresponding pairs of elements in the two sets are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equal
2 min read
LinkedHashSet retainAll() method in Java with Example
The retainAll() method of java.util.LinkedHashSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Returns Value: This method returns true
3 min read