The containsAll() method of Java AbstractCollection class

Java 8Object Oriented ProgrammingProgramming

The containsAll() method checks for all the elements in the specified collection. It returns TRUE if this collection has all the elements. The methods check for each element one by one to see if it's contained in this collection.

The syntax is as follows −

public boolean containsAll(Collection<?> c)

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

import java.util.AbstractCollection;

The following is an example to implement AbstractCollection containsAll() 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> absCollection1 = new ArrayList<Object>();
      absCollection1.add("These");
      absCollection1.add("are");
      absCollection1.add("demo");
      absCollection1.add("elements");
      System.out.println("AbstractCollection1: " + absCollection1);
      AbstractCollection<Object> absCollection2 = new ArrayList<Object>();
      absCollection2.add("These");
      absCollection2.add("are");
      absCollection2.add("demo");
      absCollection2.add("elements");
      System.out.println("AbstractCollection2: " + absCollection2);
      System.out.println("Is both the collection having identical elemenst? " + absCollection1.containsAll(absCollection2));
   }
}

Output

AbstractCollection1: [These, are, demo, elements]
AbstractCollection2: [These, are, demo, elements]
Is both the collection having identical elemenst? true
raja
Published on 27-Mar-2019 13:11:54
Advertisements