Skip to content
Related Articles
Open in App
Not now

Related Articles

Checking if Element Exists in LinkedHashSet in Java

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 11 Dec, 2020
Improve Article
Save Article

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. The contains a method of the LinkedHashSet class returns true if the specified element is found in the LinkedHashSet object.


Given List: [1, 2, 3, 4]

Input : FirstSearch = 1, SecondSearch = 6
Output: True False


Hash_Set.contains(Object element)

Return Type: If the element found in the then it returns true, else false.

Parameters: The parameter element is of the type of LinkedHashSet. This is the element that needs to be tested if it is present in the set or not.



// Checking if element exists in LinkedHashSet in Java
import java.util.LinkedHashSet;
public class LinkedHashSetContainsExample {
    public static void main(String[] args)
        LinkedHashSet<String> lhSetColors
            = new LinkedHashSet<String>();
         * To check if the LinkedHashSet contains the
         * element, use the contains method.
        // this will return true as the "red" element exists
        // this will return true as the "white" element does
        // not exists


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!