Java Collection Framework - AbstractList

Java Collection Framework - AbstractList


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

AbstractList class is an abstract class which extends the class AbstractCollection and implements the interface List. In an unmodifiable list the programmers just required to extend the class AbstractList class and allows the implementation for the methods get(int) method and size() method i.e. these methods will be implemented into their appropriate class.

Java Collection Framework - AbstractList

AbstractList class is an abstract class which extends the class AbstractCollection and implements the interface List. In an unmodifiable list the programmers just required to extend the class AbstractList class and allows the implementation for the methods get(int) method and size() method i.e. these methods will be implemented into their appropriate class. But in a modifiable list there will must be required to override the extra methods additionally. AbstractList is a part of Java collection framework and is available in java.util package.

Syntax

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

Constructor of AbstractList

protected AbstractList() : The only constructor of this class which is implicitly invoked by the subclass constructor.

Methods of AbstractList

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 Iterator<E> iterator()

  7. subList
  8.                         syntax : public List<E> subList(int fromIndex, int toIndex)

  9. remove()
  10.                         syntax : public E remove(int index)

  11. get()
  12.                         syntax : public abstract E get(int index)

Example

package devmanuals.com;

import java.util.AbstractList;

import java.util.List;

import java.util.ArrayList;

public class AbstractListDemo extends AbstractList {

  List list;

  AbstractListDemo(){

    list = new ArrayList();

  }

  public Object get(int i) {

  System.out.println(list.get(i));

    return null;

  }

  public int size() {

    System.out.println(list.size());

    return 0;

  }

  public boolean add(Object e)

  {

    list.add(e);

    System.out.println(list);

    return false;

  }

  public static void main(String... abc){

    AbstractListDemo ald= new AbstractListDemo();

    System.out.println("Elements of List =");

    for(int i=0; i<=5; i++){

      ald.add(i);  

    }

    System.out.println("Size of list =");

    ald.size();

    System.out.println("Element at position 2");

    ald.get(2);

  }

}

Output :

Elements of List =

[0]

[0, 1]

[0, 1, 2]

[0, 1, 2, 3]

[0, 1, 2, 3, 4]

[0, 1, 2, 3, 4, 5]

Size of list =

6

Element at position 2

2

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics