
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The clear() Method of Java AbstractCollection Class
The clear() method of the AbstractCollection class is used to remove all the elements from this collection. This makes the collection empty.
The syntax is as follows:
public void clear()
To work with AbstractCollection class in Java, import the following package:
import java.util.AbstractCollection;
First, create AbstractCollection and add some elements using the add() method:
AbstractCollection<Object> absCollection = new ArrayList<Object>(); absCollection.add("These"); absCollection.add("are"); absCollection.add("demo"); absCollection.add("elements");
Now, clear the AbstractCollection:
absCollection.clear();
The following is an example to implement AbstractCollection clear() method in Java:
Example
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("AbstractCollection = " + absCollection); absCollection.clear(); System.out.println("AbstractCollection after removing elements = " + absCollection); } }
Output
AbstractCollection = [These, are, demo, elements] AbstractCollection after removing elements = []
Advertisements