Sunday, July 26, 2020

List Interface In Java

List interface is an ordered collection framework and present in java.util package (also known as a sequence). It allows us to store and access elements sequentially. It extends the Collection interface.

It is a factory of ListIterator interface by using the ListIterator, we can iterate the list in forward and backward directions.

Method

Description

void add(int index, E element)

Add the specified element at the specified position (at given index) in a list.

boolean add(E e)

Append (add) the specified element at the end of a list.

boolean addAll(Collection<? extends E> c)

Append (add) all of the elements in the specified collection to the end of a list.

boolean addAll(int index, Collection<? extends E> c)

Append all the elements in the specified collection,
starting at the specified position of the list.

void clear()

Remove all of the elements from this list.

 

 

boolean equals(Object o)

Compare the specified object with the elements of a list. Return true or false

int hashcode()

Return the hash code value for a list.

E get(int index)

Fetch the element for given index of the list.

boolean isEmpty()

Returns true if the list is empty, otherwise false.

int lastIndexOf(Object o)

Return the index in the list of the last occurrence of the specified element,
or -1 if the list does not contain this element.

Object[] toArray()

Return an array containing all of the elements in the list in the same order.

<T> T[] toArray(T[] a)

Return an array containing all of the elements in the list in the same order.

boolean contains(Object o)

Returns true if the list contains the specified element

boolean containsAll(Collection<?> c)

Returns true if the list contains all the specified collection

int indexOf(Object o)

Return the index in this list of the first occurrence of the specified element,
or -1 if the List does not contain this element.

E remove(int index)

Remove the element present at the specified position (index) in the list.

boolean remove(Object o)

Remove the first occurrence of the specified element.

boolean removeAll(Collection<?> c)

Remove all the elements from the list which are present in the specified collection.

void replaceAll(UnaryOperator<E> operator)

Replace all the elements from the list with the specified element.

void retainAll(Collection<?> c)

Retain all the elements in the list that are present in the specified collection.
 And remove other elements

E set(int index, E element)

Replace the specified element in the list, present at the specified index.

void sort(Comparator<? super E> c)

Sort the elements of the list on the basis of specified comparator.
We need to pass comparator

Spliterator<E> spliterator()

Create spliterator over the elements in a list.

List<E> subList(int fromIndex, int toIndex)

Return  sublist of elements lies within the given range.

int size()

Return the number of elements in the list.

 


No comments:

Post a Comment