Sunday, January 18, 2026
HomeLanguagesJavaAbstractCollection contains() Method in Java with Examples

AbstractCollection contains() Method in Java with Examples

The contains() method of Java AbstractCollection is used to check whether an element is present in a Collection or not. It takes the element as a parameter and returns True if the element is present in the collection.
Syntax: 

AbstractCollection.contains(Object element)

Parameters: The parameter element is of type Collection. This parameter refers to the element whose occurrence is needed to be checked in the collection.
Return Value: The method returns True if the element is present in the Collection otherwise it returns False.
Below programs illustrate the Java.util.AbstractCollection.contains() method:
Program 1:

Java




// Java code to illustrate boolean contains()
 
import java.util.*;
import java.util.AbstractCollection;
 
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Collection
        AbstractCollection<String>
            abs = new LinkedList<String>();
 
        // Use add() method to add
        // elements in the collection
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
        abs.add("10");
        abs.add("20");
 
        // Displaying the collection
        System.out.println("Abstract Collection:"
                           + abs);
 
        // Check if the collection contains "Hello"
        System.out.println("\nDoes the Collection"
                           + " contains 'Hello': "
                           + abs.contains("Hello"));
 
        // Check if the Collection contains "20"
        System.out.println("Does the collection"
                           + " contains '20': "
                           + abs.contains("20"));
 
        // Check if the Collection contains "Geeks"
        System.out.println("Does the Collection"
                           + " contains 'Geeks': "
                           + abs.contains("Geeks"));
    }
}


Output: 

Abstract Collection:[Geeks, for, Geeks, 10, 20]

Does the Collection contains 'Hello': false
Does the collection contains '20': true
Does the Collection contains 'Geeks': true

 

Program 2:

Java




// Java code to illustrate boolean contains()
 
import java.util.*;
import java.util.AbstractCollection;
 
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Collection
        AbstractCollection<String>
            abs = new TreeSet<String>();
 
        // Use add() method to add
        // elements in the collection
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
        abs.add("TreeSet");
        abs.add("20");
 
        // Displaying the collection
        System.out.println("Abstract Collection:"
                           + abs);
 
        // Check if the collection contains "TreeSet"
        System.out.println("\nDoes the Collection "
                           + "contains 'TreeSet': "
                           + abs.contains("TreeSet"));
 
        // Check if the collection contains "Hello"
        System.out.println("\nDoes the Collection"
                           + " contains 'Hello': "
                           + abs.contains("Hello"));
 
        // Check if the Collection contains "20"
        System.out.println("Does the collection"
                           + " contains '20': "
                           + abs.contains("20"));
 
        // Check if the Collection contains "Geeks"
        System.out.println("Does the Collection"
                           + " contains 'Geeks': "
                           + abs.contains("Geeks"));
    }
}


Output: 

Abstract Collection:[20, Geeks, TreeSet, for]

Does the Collection contains 'TreeSet': true

Does the Collection contains 'Hello': false
Does the collection contains '20': true
Does the Collection contains 'Geeks': true

 

RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
118 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12063 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS