AbstractCollection class implements the Collection interface and this class extends the class Object. In an unmodifiable collection, the programmer shall have only required to extend the AbstractCollection class and allows the implementation for the methods iterator() and size() i.e these methods are implemented into their suitable class.
Java collection framework - AbstractCollection
AbstractCollection class implements the Collection interface and this class extends the class Object. In an unmodifiable collection, the programmer shall have only required to extend the AbstractCollection class and allows the implementation for the methods iterator() and size() i.e these methods are implemented into their suitable class. But into a modifiable collection the programmer will must have to implement the methods into this class i.e. except the above these two methods programmers must be required to override the methods additionally what they need. AbstractCollection is a part of Java collection framework and is available in java.util package.
Syntax
public abstract class AbstractCollection<E> extends Object implements Collection<E>
Constructor of AbstractCollection
AbstractCollection() : This class have the only constructor which is implicitly invoked by the subclass constructor.
Methods of AbstractCollection
This class provides various methods some of them are :
- add()
- clear()
- iterator()
- size()
- remove()
syntax : public boolean add(E e)
syntax : public void clear()
syntax : public abstract Iterator<E> iterator()
syntax : public abstract int size()
syntax : public boolean remove(Object o)
Example
package devmanuals.com; import java.util.AbstractCollection; import java.util.List; import java.util.ArrayList; import java.util.Iterator; class AbsCollection extends AbstractCollection<Object> { List<Object> list = new ArrayList<Object>(); public boolean add(Object e) { list.add(e); System.out.println(list); return true; } public Iterator iterator() { Iterator<Object> it = list.iterator(); while (it.hasNext()) System.out.println(it.next()); return null; } public int size() { System.out.println(list.size()); return 0; } } public class AbstractCollectionDemo { public static void main(String[] args) { AbsCollection abd = new AbsCollection(); System.out.println("Elements of list = "); for (int i = 0; i <= 4; i++) { abd.add(i); } System.out.println("Display of elements using Iterator "); abd.iterator(); System.out.println("Size of list = " ); abd.size(); } }
Output :
Elements of list = [0] [0, 1] [0, 1, 2] [0, 1, 2, 3] [0, 1, 2, 3, 4] Display of elements using Iterator 0 1 2 3 4 Size of list = 5 |
[ 0 ] Comments