How to add elements to AbstractCollection class in Java?

Java 8Object Oriented ProgrammingProgramming

To add elements to the AbstractCollection class, use the add() method. For example −

absCollection.add("These");
absCollection.add("are");
absCollection.add("demo");

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

import java.util.AbstractCollection;

The following is an example to add elements to AbstractCollection class 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("These");
      absCollection.add("are");
      absCollection.add("demo");
      absCollection.add("elements");
      System.out.println("Displaying elements in the AbstractCollection: " + absCollection);
   }
}

Output

Displaying elements in the AbstractCollection: [These, are, demo, elements]
raja
Published on 27-Mar-2019 13:05:26
Advertisements