Skip to content
Related Articles

Related Articles

Improve Article
AbstractCollection in Java with Examples
  • Difficulty Level : Easy
  • Last Updated : 28 Nov, 2018

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 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:

  1. add(E e): This method ensures that this collection contains the specified element (optional operation).
  2. addAll(Collection c): This method Adds all of the elements in the specified collection to this collection (optional operation).
  3. clear(): This method removes all of the elements from this collection (optional operation).
  4. contains(Object o): This method returns true if this collection contains the specified element.
  5. containsAll(Collection c): This method returns true if this collection contains all of the elements in the specified collection.
  6. isEmpty(): This method returns true if this collection contains no elements.
  7. iterator(): This method returns an iterator over the elements contained in this collection.
  8. remove(Object o): This method removes a single instance of the specified element from this collection, if it is present (optional operation).
  9. size(): This method returns the number of elements in this collection.
  10. toArray(): This method returns an array containing all of the elements in this collection.
  11. 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.
  12. toString​(): This method returns a string representation of this collection.

Example:




// 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 anothe 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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :