The contains() method of Java AbstractCollection class

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 02-Apr-2019 11:56:06
Advertisements