java.util AbstractCollection with Examples
AbstractCollection is an abstract class that extends Object and implements Collection interface. This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface. This is a member of the Java Collections Framework.
Class Hierarchy
Java.lang.Object ↳ Java.util.AbstractCollection<E>
Syntax
public abstract class AbstractCollection<E> extends Object implements Collection<E>
When you need to extend AbstractCollection ?
You need to extend this class only in order to implement an un-modifiable collection. When you extend this class provide implementations for iterator and size methods (The iterator returned by the iterator method must implement hasNext and next) as shown below.
package com.sneppets.java.util; import java.util.AbstractCollection; import java.util.Iterator; /** * Program sneppet to demonstrate how to implement un-modifiable collection * @author sneppets.com * */ public class UnModifiableStringCollection<E extends Comparable<E>> extends AbstractCollection<String>{ @Override public StringIterator<String> iterator() { return new StringIterator<>(); } @Override public int size() { return 0; } } class StringIterator<E extends Comparable<String>> implements Iterator<String>{ @Override public boolean hasNext() { return false; } @Override public String next() { return null; } }
How to implement modifiable collection ?
If you want to implement a modifiable collection, then you should override AbstractCollection’s add() method and the iterator returned by the iterator method must implement its remove method additionally.
Methods of AbstractCollection
- add(E e): This method ensures that this collection contains the specified element (optional operation).
- addAll(Collection c): This method Adds all of the elements in the specified collection to this collection (optional operation).
- clear(): This method removes all of the elements from this collection (optional operation).
- contains(Object o): This method returns true if this collection contains the specified element.
- containsAll(Collection c): This method returns true if this collection contains all of the elements in the specified collection.
- isEmpty(): This method returns true if this collection contains no elements.
- iterator(): This method returns an iterator over the elements contained in this collection.
- remove(Object o): This method removes a single instance of the specified element from this collection, if it is present (optional operation).
- removeAll(Collection<?> c): Removes all of this collection’s elements that are also contained in the specified collection (optional operation).
- retainAll(Collection<?> c): Retains only the elements in this collection that are contained in the specified collection (optional operation).
- size(): This method returns the number of elements in this collection.
- toArray(): This method returns an array containing all of the elements in this collection.
- 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.
- toString(): This method returns a string representation of this collection.
Related Posts
- Java Enumeration Interface and Vector Example
- ArrayDeque – Java Example
- Collection(s) and Collections Framework