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:
- Class hierarchy
- Syntax
- Constructor
- Methods
- An example program to understand AbstractCollection
- 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:
- boolean add(E obj): It adds the specified object to the collection.
- boolean addAll(Collection c): Adds all the elements from one collection to the other and return a true value.
- boolean remove(Object obj): It removes the specified object from the collection.
- void clear(): Removes all the elements from the current Collection.
- boolean removeAll(Collection c): Removes all elements in the current Collection of each object in the specified Collection and returns a true value.
- abstract int size(): Returns number of objects in the Collection.
- boolean contains(Object obj): Returns true if the collection contains obj Object.
- boolean containsAll(Collection c): Returns true if this collection contains all the elements in a given Collection.
- abstract Iterator<E> iterator(): Returns an iterator over the current Collection.
- boolean isEmpty(): Checks whether the Collection is empty.
- boolean retainAll(Collection C): Removes from the current Collection, all its elements that are not contained in a given Collection.
- Object[ ] toArray(): Returns a new array containing all the elements of the current Collection.
- 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