AbstractCollection clear() Method in Java with Examples
The clear() method of Java AbstractCollection in Java is used to remove all of the elements from the Collection. Using the clear() method only clears all the element from the collection and does not delete the collection. In other words, it can be said that the clear() method is used to only empty an existing AbstractCollection.
Syntax:
AbstractCollection.clear()
Return Value: The function does not return any value.
Below programs illustrate the AbstractCollection.clear() method:
Program 1:
// Java code to illustrate clear(Object o) // of AbstractCollelction import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String[] args) { // Create an empty Collection AbstractCollection<Object> abs = new ArrayList<Object>(); // Use add() method to add // elements in the collection abs.add("Welcome"); abs.add("To"); abs.add("Geeks"); abs.add("4"); abs.add("Geeks"); // Displaying the Collection System.out.println("AbstractCollection: " + abs); // Clearing the Collection abs.clear(); // Displaying the Collection System.out.println("AbstractCollection " + "after using clear: " + abs); } } |
AbstractCollection: [Welcome, To, Geeks, 4, Geeks] AbstractCollection after using clear: []
Program 2:
// Java code to illustrate clear(Object o) // of AbstractCollelction import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String[] args) { // Create an empty collection AbstractCollection<Object> abs = new LinkedList<Object>(); // Use add() method to add // elements in the collection abs.add(15); abs.add(20); abs.add(25); abs.add(30); abs.add(35); // Displaying the Collection System.out.println("AbstractCollection: " + abs); // Clearing the Collection abs.clear(); // Displaying the Collection System.out.println("AbstractCollection " + "after using clear: " + abs); } } |
AbstractCollection: [15, 20, 25, 30, 35] AbstractCollection after using clear: []
Recommended Posts:
- AbstractCollection contains() Method in Java with Examples
- AbstractCollection add() Method in Java with Examples
- AbstractCollection toString() method in Java with Examples
- AbstractCollection containsAll() Method in Java with Examples
- AbstractCollection remove() Method in Java with Examples
- AbstractCollection isEmpty() Method in Java with Examples
- AbstractCollection addAll() Method in Java with Examples
- AbstractCollection size() Method in Java with Examples
- AbstractCollection retainAll() method in Java with Examples
- AbstractCollection toArray() Method in Java with Examples
- Set clear() method in Java with Examples
- Calendar clear() Method in Java with Examples
- EnumMap clear() Method in Java with Examples
- AbstractList clear() method in Java with Examples
- AbstractMap clear() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



