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

AbstractCollection In Java

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

CLASS HIERARCHY:

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

SYNTAX:

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

CONSTRUCTOR:

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.

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
        abc.add(1); 
        abc.add(2); 
        abc.add(3); 
        abc.add(4); 
        abc.add(5); 
       
        
        // 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
        abc.remove(3);
        abc.remove(1);
        System.out.println("AbstractCollection abc after removing the objects: "
                           + abc); 
        
        //clear() METHOD
        abc.clear();
        System.out.println("Is the collection abc empty? "
                           + abc.isEmpty()); 
        
        
        abc.add(1); 
        abc.add(2); 
        abc.add(3); 
        abc.add(4); 
        abc.add(5); 
                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
        c.addAll(abc);
                        System.out.println("AbstractCollection c: "
                           +c); 
                        
                        //containsAll() METHOD
                        System.out.println("AbstractCollection c = AbstractCollection abc?: "
                           +c.containsAll(abc));
                        
        
    } 
}

 

 

Output:

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 *