AbstractCollection in Java

Hello everyone! so, in this tutorial, we will learn about AbstractCollection in Java.
It is used to implement an unmodifiable collection.

Table of contents:

  1. Class hierarchy
  2. Syntax
  3. Constructor
  4. Methods
  5. An example program to understand AbstractCollection
  6. The output of the program

So what is a collection?

In the real world, it means similar objects grouped together.

This is exactly what it means in java programming. You must already know about the collections we commonly use such as the array, linked lists, structures, etc.

An abstract collection is similar to the other collections. So where ever you go you’ll find websites telling you that “Class AbstractCollection¬†is an abstract implementation of the collection interface.¬†” But before we learn about it we ‘ll have to learn about “interface”.

An interface is similar to a class but it just has methods and variable declarations, no definitions. It just tells what a class should be not how to be it. This is basically a blueprint for a class.

Similarly, a “Collection” interface contains a few core methods of Java and “Abstract Collection” is used to easily implement this interface.

Few methods that are contained in the Abstract Collection are:

  1. add()
  2. clear()
  3. isEmpty()
  4. remove(Object)
  5. size()
// Java code to illustrate AbstractCollection 

import java.util.*; 
import java.util.AbstractCollection; //we have to call the abstract collection separately

public class Abstract
	public static void main(String args[]) 
		AbstractCollection<Object> absclc = new LinkedList<Object>();  //abs is an object of the class Abstract Collection 
		absclc.add("Hello"); //we ae only illustracting the use of add()

		System.out.println(absclc); //the output is shown in a list fashion

The output of the following code:

[Hello, To, You]

AbstractCollection In Java

AbstractCollection is a part of the Java Collection Framework.
It implements the Collection interface.


java.lang.Object -> java.util -> class AbstractCollection<T>


public abstract class AbstractCollection<T> extends Object implements Collection<T>


protected AbstractCollection()

Constructs a new instance of AbstractCollection.
As the Access Modifier is protected, the AbstractCollection object cannot be created.

Methods of AbstractCollection:

  1. boolean add(E obj): It adds the specified object to the collection.
  2. boolean addAll(Collection c): Adds all the elements from one collection to the other and return a true value.
  3. boolean remove(Object obj): It removes the specified object from the collection.
  4. void clear(): Removes all the elements from the current Collection.
  5. boolean removeAll(Collection c): Removes all elements in the current Collection of each object in the specified Collection and returns a true value.
  6. abstract int size(): Returns number of objects in the Collection.
  7. boolean contains(Object obj): Returns true if the collection contains obj Object.
  8. boolean containsAll(Collection c): Returns true if this collection contains all the elements in a given Collection.
  9. abstract Iterator<E> iterator(): Returns an iterator over the current Collection.
  10. boolean isEmpty(): Checks whether the Collection is empty.
  11. boolean retainAll(Collection C): Removes from the current Collection, all its elements that are not contained in a given Collection.
  12. Object[ ] toArray(): Returns a new array containing all the elements of the current Collection.
  13. Object[ ] toArray(Object[ ] z): Returns an array containing all the elements of the current Collection.

Also check: Unmodifiable Collection in Java

Example of AbstractCollection

So, let us understand this by a simple example.

package javaapplication21;
import java.util.*; 

public class JavaApplication21 { 
    public static void main(String[] args) 
        AbstractCollection<Object> abc = new ArrayList<>(); 
        //isEmpty() METHOD
        System.out.println("Is the collection abc empty? "
                           + abc.isEmpty()); 
        //size() METHOD
        System.out.println("The size of the Collection abc before adding the objects: "
                           + abc.size()); 
        //add() METHOD
        // Displaying the Collection 
        System.out.println("AbstractCollection abc: "
                           + abc); 
        System.out.println("Is the collection abc empty? "
                           + abc.isEmpty()); 
        System.out.println("The size of the Collection abc after adding the objects: "
                           + abc.size()); 
        //conatins() METHOD
        System.out.println("Is 5 present in the collection abc?"+abc.contains(5));
        //remove() METHOD
        System.out.println("AbstractCollection abc after removing the objects: "
                           + abc); 
        //clear() METHOD
        System.out.println("Is the collection abc empty? "
                           + abc.isEmpty()); 
                System.out.println("AbstractCollection abc after adding objects: "
                           + abc); 
        System.out.println("Collection abc conatains 5?: "
                           + abc.contains(5));
        AbstractCollection<Object> c = new ArrayList<>();
        //addAll() METHOD
                        System.out.println("AbstractCollection c: "
                        //containsAll() METHOD
                        System.out.println("AbstractCollection c = AbstractCollection abc?: "




Is the collection empty? true
The size of the Collection before adding the obejcts: 0
AbstractCollection: [1, 2, 3, 4, 5]
Is the collection empty? false
The size of the Collection after adding the obejcts: 5
Is 5 present in the collection?true
AbstractCollection after removing the objects: [2, 4, 5]
Is the collection empty? true
AbstractCollection after adding objects: [1, 2, 3, 4, 5]
Collection conatains 5?: true
AbstractCollection c: [1, 2, 3, 4, 5]
AbstractCollection c = AbstractCollection abc?: true

So, in the above example, we have implemented the methods of AbstarctCollection.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *