Thursday, 20 October 2016

What is AbstractList In Java ?

Previously we have discussed about 
Here we learn what is AbstractList In Java.But before that we must know, What is Collection Interface in Java ?
Class AbstractList<E>

All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>

Direct Known Subclasses:
AbstractSequentialList, ArrayList, Vector

Declaration of AbstractList:
public abstract class AbstractList<E>
extends AbstractCollection<E>
implements List<E>

AbstractList Class is an skeletal implemetation of List interface.For sequential access data (such as a Linked list), AbstractSequentialList should be used in preference to this class.

To implement an unmodifiable list, the programmer needs only to extend this class and provide implementations for the get(int) and size() methods.

To implement a modifiable list, the programmer must additionally override the set(int, E) method. If the list is variable-size the programmer must additionally override the add(int, E) and remove(int) methods.

Unlike the other abstract collection implementations, the programmer does not have to provide an iterator implementation; the iterator and list iterator are implemented by this class, on top of the "random access" methods: get(int), set(int, E), add(int, E) and remove(int).
This class is a member of the Java Collections Framework.

Constructor Summary of AbstractList:

Constructor
Description
AbstractList()
Sole constructor.

Method Summary of AbstractList:

Modifier and Method Name
Description
Boolean add(E e)
Appends the specified element to the end of this list (optional operation).
Void add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).
Boolean addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
Void clear()
Removes all of the elements from this list (optional operation).
Boolean equals(Object o)
Compares the specified object with this list for equality.
abstract E get(int index)
Returns the element at the specified position in this list.
Int hashCode()
Returns the hash code value for this list.
Int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
Int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
ListIterator<E>  listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E>  listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E remove(int index)
Removes the element at the specified position in this list (optional operation).
protected void removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
E set(int index, E element)
Replaces the element at the specified position in this list with the specified element (optional operation).
List<E>  subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
      
Example of AbstractList:

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

class Customer extends AbstractList<String>{

List<String> list = new ArrayList<String>();

@Override
public String get(int index) {

return list.get(index);
}
@Override
public int size() {

return list.size();
}
@Override
public boolean add(String element) {
list.add(element);
System.out.println(element);
return false;
}
}
public class AbstractListExample {

public static void main(String[] args) {
Customer c = new Customer();

for(int i=0;i<5;i++){
c.add("Element_"+i);
}
System.out.println("Size of List : "+c.size());
System.out.println("Element at Position 2 : "+c.get(2));
}
}
Program Output:

Element_0
Element_1
Element_2
Element_3
Element_4
Size of List : 5
Element at Position 2 : Element_2



Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment