The contains() method of Java AbstractCollection class


The contains() method of the AbstractCollection class checks whether an element is in the AbstractCollection or not. It returns a Boolean i.e. TRUE if the element is in the collection, else FALSE is returned.

The syntax is as follows:

public boolean contains(Object ele)

Here, ele is the element to be checked for existence.

To work with AbstractCollection class in Java, import the following package:

import java.util.AbstractCollection;

The following is an example to implement AbstractCollection contains() method in Java:

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractCollection;
public class Demo {
   public static void main(String[] args) {
      AbstractCollection<Object> absCollection = new ArrayList<Object>();
      absCollection.add("Football");
      absCollection.add("Tennis");
      absCollection.add("Badminton");
      absCollection.add("Cricket");
      absCollection.add("Basketball");
      absCollection.add("Golf");
      absCollection.add("BaseBall");
      absCollection.add("Handball");
      System.out.println("AbstractCollection = " + absCollection);
      System.out.println("The elements exist in the Collection? = "+ absCollection.contains("Golf"));
   }
}

Output

AbstractCollection = [Football, Tennis, Badminton, Cricket, Basketball, Golf, BaseBall, Handball]
The elements exist in the Collection? = true

Updated on: 30-Jul-2019

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements