LinkedHashSet contains() method in Java with Examples
The contains() method of java.util.LinkedHashSet class is used to return true if this set contains the specified element otherwise false.
Syntax:
public boolean contains(Object o)
Parameters: This method takes element o as parameter whose presence in this set is to be tested.
Return Value: This method returns true if this set contains the specified element.
Below are the examples to illustrate the contains() method.
Example 1:
// Java program to demonstrate // contains() method // for String value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Create the object of LinkedHashSet<String> LinkedHashSet<String> linkset = new LinkedHashSet<String>(); // populate hash set linkset.add("A"); linkset.add("B"); linkset.add("C"); // print the LinkedHashSet System.out.println("LinkedHashSet: " + linkset); // check the existence of element // using method contains() boolean exist = linkset.contains("C"); // print the result System.out.println("Is the element" + " 'C' present: " + exist); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
LinkedHashSet: [A, B, C] Is the element 'C' present: true
Example 2:
// Java program to demonstrate // contains() method // for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Create the object of LinkedHashSet<Integer> LinkedHashSet<Integer> linkset = new LinkedHashSet<Integer>(); // populate hash set linkset.add(10); linkset.add(20); linkset.add(30); // print the LinkedHashSet System.out.println("LinkedHashSet: " + linkset); // check the existence of element // using method contains() boolean exist = linkset.contains(25); // print the result System.out.println("Is the element" + " '25' present: " + exist); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
LinkedHashSet: [10, 20, 30] Is the element '25' present: false
Recommended Posts:
- LinkedHashSet add() method in Java with Examples
- LinkedHashSet clear() method in Java with Examples
- LinkedHashSet in Java with Examples
- LinkedHashSet contains() method in Java
- LinkedHashSet removeAll() method in Java with Example
- LinkedHashSet clone() method in Java
- LinkedHashSet remove() method in Java
- LinkedHashSet size() method in Java
- LinkedHashSet isEmpty() method in Java
- LinkedHashSet toArray(T[]) method in Java with Example
- LinkedHashSet equals() method in Java with Example
- LinkedHashSet retainAll() method in Java with Example
- LinkedHashSet containsAll() method in Java with Example
- LinkedHashSet toArray() method in Java with Example
- LinkedHashSet toString() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


