
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 = []
- Related Questions & Answers
- The toString() method of Java AbstractCollection class
- The toArray() method of Java AbstractCollection class
- The containsAll() method of Java AbstractCollection class
- The isEmpty() method of Java AbstractCollection class
- The iterator() method of Java AbstractCollection class
- The remove() method of Java AbstractCollection class
- The size() method of Java AbstractCollection class
- The addAll() method of Java AbstractCollection class
- The contains() method of Java AbstractCollection class
- The toArray(T[] a) T method of Java AbstractCollection class
- The clear() method of AbstractList class in Java
- The clear() method of CopyOnWriteArrayListin Java
- What is AbstractCollection class in Java?
- The clear() method of AbstractSequentialList in Java
- Java IdentityHashMap clear() method
Advertisements