Java Collection Framework - AbstractCollection

Java Collection Framework - AbstractCollection


Posted in : Core Java Posted on : May 25, 2011 at 10:56 AM Comments : [ 0 ]

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 :

  1. add() 
  2.                         syntax : public boolean add(E e)

  3. clear()  
  4.                         syntax : public void clear()

  5. iterator()
  6.                         syntax : public abstract Iterator<E> iterator()

  7. size()
  8.                         syntax : public abstract int size()

  9. remove()
  10.                         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

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics