Syntax error How to add elements to AbstractCollection class in Java?

How to add elements to AbstractCollection class in Java?



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]
Updated on: 2019-07-30T22:30:25+05:30

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements