LinkedHashSet contains() Method in Java with Examples
Last Updated :
18 Nov, 2021
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
import java.util.*;
public class GFG {
public static void main(String[] argv) throws Exception
{
try {
LinkedHashSet<String> linkset
= new LinkedHashSet<String>();
linkset.add( "A" );
linkset.add( "B" );
linkset.add( "C" );
System.out.println( "LinkedHashSet: " + linkset);
boolean exist = linkset.contains( "C" );
System.out.println( "Is the element"
+ " 'C' present: " + exist);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
LinkedHashSet: [A, B, C]
Is the element 'C' present: true
Example 2:
Java
import java.util.*;
public class GFG {
public static void main(String[] argv) throws Exception
{
try {
LinkedHashSet<Integer> linkset
= new LinkedHashSet<Integer>();
linkset.add( 10 );
linkset.add( 20 );
linkset.add( 30 );
System.out.println( "LinkedHashSet: " + linkset);
boolean exist = linkset.contains( 25 );
System.out.println( "Is the element"
+ " '25' present: " + exist);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
LinkedHashSet: [10, 20, 30]
Is the element '25' present: false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...