AbstractCollection in Java with Examples Last Updated : 29 Nov, 2022 Comments Improve Suggest changes Like Article Like Report The AbstractCollection class in Java is a part of the Java Collection Framework and implements the Collection interface. It is used to implement an unmodifiable collection, for which one needs to only extend this AbstractCollection Class and implement only the iterator and the size methods. Class Hierarchy: java.lang.Object ↳ java.util ↳ Class AbstractCollection<E> Syntax: public abstract class AbstractCollection<E> extends Object implements Collection<E> where E is the type of elements maintained by this collection. Constructors in Java AbstractCollection: protected AbstractCollection(): The default constructor, but being protected, it doesn't allow to create an AbstractCollection object. Below is the sample program to illustrate AbstractCollection in Java: Java // Java code to illustrate AbstractCollection import java.util.*; import java.util.AbstractCollection; public class GFG { 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); } } Output:AbstractCollection: [Welcome, To, Geeks, 4, Geeks] Methods in Java Abstract Collection: add(E e): This method ensures that this collection contains the specified element (optional operation).addAll(Collection c): This method Adds all of the elements in the specified collection to this collection (optional operation).clear(): This method removes all of the elements from this collection (optional operation).contains(Object o): This method returns true if this collection contains the specified element.containsAll(Collection c): This method returns true if this collection contains all of the elements in the specified collection.isEmpty(): This method returns true if this collection contains no elements.iterator(): This method returns an iterator over the elements contained in this collection.remove(Object o): This method removes a single instance of the specified element from this collection, if it is present (optional operation).size(): This method returns the number of elements in this collection.toArray(): This method returns an array containing all of the elements in this collection.toArray(T[] a): This method returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.toString(): This method returns a string representation of this collection. Example: Java // Java code to illustrate // methods of AbstractCollection import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String args[]) { // Creating an empty collection AbstractCollection<String> abs1 = new TreeSet<String>(); // Use add() method to add // elements into the Collection abs1.add("Welcome"); abs1.add("To"); abs1.add("Geeks"); abs1.add("4"); abs1.add("Geeks"); abs1.add("TreeSet"); // Displaying the Collection System.out.println("AbstractCollection 1: " + abs1); // Creating another Collection AbstractCollection<String> abs2 = new TreeSet<String>(); // Displaying the Collection System.out.println("AbstractCollection 2: " + abs2); // Using addAll() method to Append abs2.addAll(abs1); // Displaying the Collection System.out.println("AbstractCollection 2: " + abs2); // Clearing the collection // using clear() method abs1.clear(); // Check for the empty collection System.out.println("Is the collection empty? " + abs1.isEmpty()); } } Output:AbstractCollection 1: [4, Geeks, To, TreeSet, Welcome] AbstractCollection 2: [] AbstractCollection 2: [4, Geeks, To, TreeSet, Welcome] Is the collection empty? true Reference: https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html Comment More infoAdvertise with us Next Article AbstractCollection add() Method in Java with Examples R RishabhPrabhu Follow Improve Article Tags : Java Java-Collections Java - util package Java-AbstractCollection Practice Tags : JavaJava-Collections Similar Reads AbstractCollection in Java with Examples The AbstractCollection class in Java is a part of the Java Collection Framework and implements the Collection interface. It is used to implement an unmodifiable collection, for which one needs to only extend this AbstractCollection Class and implement only the iterator and the size methods. Class Hi 3 min read AbstractCollection add() Method in Java with Examples The add() method in Java AbstractCollection is used to add a specific element into a Collection. This method will add the element only if the specified element is not present in the Collection else the function will return False if the element is already present in the Collection. Syntax: AbstractCo 2 min read AbstractCollection addAll() Method in Java The addAll() method of Java AbstractCollection is used to append all elements from a given collection to the current collection. If the collection being appended is a TreeSet, the elements are stored in sorted order, as TreeSet maintains a natural ordering. It is important to note that AbstractColle 2 min read 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 a 2 min read AbstractCollection contains() Method in Java with Examples The contains() method of Java AbstractCollection is used to check whether an element is present in a Collection or not. It takes the element as a parameter and returns True if the element is present in the collection.Syntax: AbstractCollection.contains(Object element) Parameters: The parameter eleme 2 min read AbstractCollection containsAll() Method in Java The containsAll() method of Java AbstractCollection is used to check whether a collection contains all elements of another collection.Example 1: This program checks if two collections have the same elements using AbstractCollection.containsAll() method.Java// Java Program to illustrate containsAll() 2 min read AbstractCollection isEmpty() Method in Java with Examples The isEmpty() method of Java AbstractCollection is used to check and verify if a Collection is empty or not. It returns True if the Collection is empty else it returns False. Syntax: AbstractCollection.isEmpty() Parameters: The method does not take any parameter. Return Value: The function returns T 2 min read AbsractCollection iterator() Method in Java with Examples The iterator() method of Java AbstractCollection is used to return an iterator of the same elements as that of the Collection. The elements are returned in random order from what was present in the Collection. Syntax: Iterator iterate_value = AbstractCollection.iterator(); Parameters: The function d 2 min read AbstractCollection remove() Method in Java with Examples The remove(Object O) method of Java AbstractCollection is to remove a particular element from a Collection. Syntax: AbstractCollection.remove(Object O) Parameters: The parameter O is of the type of Collection and specifies the element to be removed from the collection. Return Value: This method retu 2 min read AbstractCollection size() Method in Java with Examples The size() method of Java AbstractCollection is used to get the size of the Collection or the number of elements present in the Collection.Syntax: AbstractCollection.size() Parameters: The method does not take any parameter.Return Value: The method returns the size or the number of elements present 2 min read Like