Skip to main content

AbstractCollection in java

java.util.AbstractCollection is an abstract class in Java that provides a skeletal implementation of the Collection interface. It is intended to be used as a base class for custom implementations of the Collection interface.

AbstractCollection provides implementations of many of the methods of the Collection interface, including size(), isEmpty(), contains(Object), iterator(), toArray(), add(E), remove(Object), and containsAll(Collection<?>). However, it does not provide implementations for the addall(), removeAll(), retainAll(), and clear() methods, which must be implemented by the subclass.

Subclasses of AbstractCollection must provide an implementation for the iterator() method, which returns an Iterator object for iterating over the elements in the collection. They must also implement the size() method to return the number of elements in the collection.

AbstractCollection is a useful class to extend when creating custom collection classes because it provides a basic implementation of many of the methods in the Collection interface, which can save time and effort in implementing them from scratch.

Inside of this class exists 14 public methods. Declaration of this class as follows.

public abstract class java.util.AbstractCollection <E> 
implements java.util.Collection <E>

AbstractCollection public method

There are following useful methods which is define the inside of java.util.AbstractCollection class.

MethodDescription
boolean add(E e) It ensures that this collection contains the specified element (optional operation).
boolean addAll(Collection c) It adds all of the elements in the specified collection to this collection (optional operation).
void clear() It removes all of the elements from this collection (optional operation).
boolean contains(Object o) It returns true if this collection contains the specified element.
boolean containsAll(Collection c) It returns true if this collection contains all of the elements in the specified collection.
boolean isEmpty() It returns true if this collection contains no elements.
abstract Iterator iterator() It returns an iterator over the elements contained in this collection.
boolean remove(Object o) It removes a single instance of the specified element from this collection, if it is present (optional operation).
boolean removeAll(Collection c) It removes all of this collection's elements that are also contained in the specified collection (optional operation).
boolean retainAll(Collection c) It retains only the elements in this collection that are contained in the specified collection (optional operation).
abstract int size() It returns the number of elements in this collection.
Object[] toArray() It returns an array containing all of the elements in this collection.
 T[] toArray(T[] a) It returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
String toString() It returns a string representation of this collection.

This reference is belong to javadoc

This is an abstract class so it's not create any instance. But this class variable can hold the reference of subclass instance. Those class is AbstractList, ArrayDeque and ConcurrentLinkedDeque.

Example By using AbstractList

// Useful package
import java.util.AbstractCollection;
import java.util.ArrayList;
public class Example
{
	public static void main(String[] args)
	{
		// Empty collection
		AbstractCollection < Object > record = new ArrayList < Object > ();
		// Add element
		record.add("Is");
		record.add("Real");
		record.add(10);
		record.add("errors");
		// Display value
		System.out.println(record);
	}
}
AbstractCollection using ArrayList
[Is, Real, 10, errors]

Example By using ArrayDeque

// Useful package
import java.util.AbstractCollection;
import java.util.ArrayDeque;
public class Example
{
	public static void main(String[] args)
	{
		// Empty collection
		AbstractCollection < Object > record = new ArrayDeque < Object > ();
		// Add element
		record.add("Is");
		record.add("Real");
		record.add(10);
		record.add("errors");
		// Display value
		System.out.println(record);
	}
}
AbstractCollection using ArrayDeque
[Is, Real, 10, errors]

Example By using ConcurrentLinkedDeque

// Useful package
import java.util.AbstractCollection;
import java.util.concurrent.ConcurrentLinkedDeque;
public class Example
{
	public static void main(String[] args)
	{
		// Empty collection
		AbstractCollection < Object > record = 
          new ConcurrentLinkedDeque < Object > ();
		// Add element
		record.add("Is");
		record.add("Real");
		record.add(10);
		record.add("errors");
		// Display value
		System.out.println(record);
	}
}
AbstractCollection using ConcurrentLinkedDeque
[Is, Real, 10, errors]




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment