The remove() method of Java AbstractCollection class

Java 8Object Oriented ProgrammingProgramming

If you want to remove an element from the AbstractCollection classs, use the remove() method. It returns TRUE if the elements requested to be removed is successfully removed from the collection.

The syntax is as follows:

public boolean remove(Object ob)

Here, ob is the element to be removed the from this Collection. Whereas, the class Object is the root of the class hierarchy.

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

import java.util.AbstractCollection;

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

Example

 Live Demo

import java.util.Iterator;
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("Laptop");
      absCollection.add("Tablet");
      absCollection.add("Mobile");
      absCollection.add("E-Book Reader");
      absCollection.add("SSD");
      absCollection.add("HDD");
      System.out.println("AbstractCollection = " + absCollection);
      absCollection.remove("Tablet");
      absCollection.remove("SSD");
      System.out.println("Collection after removing some 2 elements = " + absCollection);
   }
}

Output

AbstractCollection = [Laptop, Tablet, Mobile, E-Book Reader, SSD, HDD]
Collection after removing some elements = [Laptop, Mobile, E-Book Reader, HDD]
raja
Published on 01-Apr-2019 12:56:17
Advertisements