java util AbstractCollection Class
java.util.AbstractCollection Class
AbstractCollection Class in Java
The AbstractCollection class provides a skeletal implementation of the Collection interface. To implement an unmodifiable collection, the programmer needs only to extend AbstractCollection class and provide implementations for the iterator and size methods. (The iterator returned by the iterator method must implement hasNext and next.)
To implement a modifiable collection, the programmer must additionally override AbstractCollection class's add method (which otherwise throws an UnsupportedOperationException), and the iterator returned by the iterator method must additionally implement its remove method.
The programmer should generally provide a void (no argument) and Collection constructor, as per the recommendation in the Collection interface specification.
java.util.AbstractCollection class Example
/* Java AbstractCollection class Example Save with file name AbstractCollectionExample.java */ import java.util.AbstractCollection; import java.util.ArrayList; public class AbstractCollectionExample { public static void main(String args[]) { // java.util.AbstractCollection DECLARATION AbstractCollection al; // java.util.AbstractCollection OBJECT CREATION al = new ArrayList(); // ADD AN ELEMENT al.add("AbstractCollection Tutorial"); al.add("Huda Tutorials"); al.add("Java Tutorials"); // DUPLICATES ARE ALLOWED al.add("Huda Tutorials"); al.add("C Tutorials"); al.add("CPP Tutorials"); // null ALLOWED al.add(null); // RETURNS COUNT OF ELEMENTS AbstractCollection CONTAINS System.out.println("Elements Count : " + al.size()); // java.util.AbstractCollection OUTPUT System.out.println(al); } }