AbstractList class in java

java.util.AbstractList is a class in java programming language. Inside of this class exists 15 public methods. Declaration of this class as follows.

public abstract class java.util.AbstractList <E > 
extends java.util.AbstractCollection <E > 
implements java.util.List <E > 

AbstractList public method

There are following useful methods which is define the inside of java.util.AbstractList class.

MethodDescription
boolean add(E e) It appends the specified element to the end of this list (optional operation).
void add(int index, E element) It inserts the specified element at the specified position in this list (optional operation).
boolean addAll(int index, Collection c) It inserts all of the elements in the specified collection into this list at the specified position (optional operation).
void clear() It removes all of the elements from this list (optional operation).
boolean equals(Object o) It compares the specified object with this list for equality.
abstract E get(int index) It returns the element at the specified position in this list.
int hashCode() It returns the hash code value for this list.
int indexOf(Object o) It 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 iterator() It returns an iterator over the elements in this list in proper sequence.
int lastIndexOf(Object o) It 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 listIterator() It returns a list iterator over the elements in this list (in proper sequence).
ListIterator listIterator(int index) It 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) It removes the element at the specified position in this list (optional operation).
protected void removeRange(int fromIndex, int toIndex) It removes from this list all of the elements whose index is between , inclusive, and toIndex, exclusive.
E set(int index, E element) It replaces the element at the specified position in this list with the specified element (optional operation).
List subList(int fromIndex, int toIndex) It returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

This reference is belong to javadoc.

This is an abstract class so it's not create any instance. But this class variable can hold the reference of subclass instance. Those class is Vector, ArrayList and LinkedList.

Example By using Vector

// Example 1
import java.util.AbstractList;
import java.util.Vector;
public class Example
{
	public static void main(String args[])
	{
		// New empty Vector
		AbstractList < String > record = new Vector < String > ();
		// Add value
		record.add("Write");
		record.add("A");
		record.add("Code");
		System.out.println(record);
	}
}
AbstractList using Vector
[Write, A, Code]

Example By using ArrayList

// Example 2
import java.util.AbstractList;
import java.util.ArrayList;
public class Example
{
	public static void main(String args[])
	{
		// New empty ArrayList
		AbstractList < String > record = new ArrayList < String > ();
		// Add value
		record.add("Write");
		record.add("A");
		record.add("Code");
		System.out.println(record);
	}
}
AbstractList using ArrayList
[Write, A, Code]

Example By using LinkedList

// Example 3
import java.util.AbstractList;
import java.util.LinkedList;
public class Example
{
	public static void main(String args[])
	{
		// New empty LinkedList
		AbstractList < String > record = new LinkedList < String > ();
		// Add value
		record.add("Write");
		record.add("A");
		record.add("Code");
		System.out.println(record);
	}
}
AbstractList using LinkedList
[Write, A, Code]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved