The List Interface
A List is an ordered Collection, sometimes called a sequence.
Lists may contain duplicate elements. In addition to the operations inherited
from Collection, the List interface includes operations for the
following:
·
Positional access — manipulates
elements based on their numerical position in the list. This includes methods
such as get, set, add, addAll, and remove.
·
Search — searches for a specified
object in the list and returns its numerical position. Search methods include indexOf and lastIndexOf.
·
Iteration — extends Iterator behaviour
to take advantage of the list's sequential nature. The listIterator methods
provide this behavior.
·
Range-view — The sublist method
performs arbitrary range operations on the list.
The Java platform contains two general- purpose List implementations.
·
ArrayList, which is usually the better-performing
implementation
·
LinkedList which offers better performance
under certain circumstances.
ArrayList
|
LinkedList
|
ArrayList internally
uses dynamic array to store the elements.
|
LinkedList internally uses doubly linked list to store the
elements.
|
Manipulation with
ArrayList is slow because it internally uses array. If any element
is removed from the array, all the bits are shifted in memory.
|
Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so
no bit shifting is required in memory.
|
ArrayList class can act
as a list only because it implements List only.
|
LinkedList class can act as a list and queue both because it implements List and
Deque interfaces.
|
ArrayList is better
for storing and accessing data.
|
LinkedList is better for
manipulating data.
|
Important
methods of List Interface
Method
|
Description
|
Boolean
add(Object obj)
|
Appends the specified element to the end of this
list
|
void add(int index,
Object obj)
|
Inserts the specified element at the specified position in this list
|
boolean addAll(int
index, Collection c)
|
Inserts all elements of c into the invoking list at the
index passed in the index. Returns true if the invoking list changes and
returns false otherwise.
|
Object get(int index)
|
Returns the object stored at the specified index within the invoking
collection.
|
int indexOf(Object obj)
|
Returns the index of the first occurrence of the
specified element in this list, or -1 if this list does not contain the
element.
|
int lastIndexOf(Object
obj)
|
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( )
|
Returns an iterator to the start of the invoking
list.
|
ListIterator
listIterator(int index)
|
Returns an iterator to the invoking list that begins at the specified
index.
|
Object remove(int
index)
|
Removes the element at position index from the
invoking list and returns the deleted element.
|
Object set(int index,
Object obj)
|
Assigns obj to the location specified by index within the invoking
list.
|
List subList(int start,
int end)
|
Returns a view of the portion of this list
between the specified fromIndex, inclusive, and toIndex, exclusive.
|
No comments:
Post a Comment