List Interface
List Interface is the subinterface of Collection. It contains index-based methods to insert and delete elements. It is a factory of ListIterator interface. Lists may contain duplicate elements.
The Java platform contains two general-purpose List implementations. ArrayList, which is usually the better-performing implementation, and LinkedList which offers better performance under certain circumstances.
List interface includes different operations:
- # Query Operations
- # Modification Operations
- # Bulk Modification Operations
- # Comparison and hashing
- # Positional Access Operations
- # Search Operations
- # List Iterators
- # view
Let’s see the List Interface from java.util pacjage:
package java.util;
import java.util.function.UnaryOperator;
public interface List<E> extends Collection<E> {
// Query Operations
int size(); /**Returns the number of elements in this list. */
boolean isEmpty(); /** Returns true if this list contains no elements. */
boolean contains(Object o); /** Returns true if this list contains the specified element. */
Iterator<E> iterator(); /**Returns an iterator over the elements in this list in proper sequence. */
Object[] toArray(); /*Returns an array containing all of the elements in this list in proper sequence (from first to last element). */
<T> T[] toArray(T[] a); /**Returns an array containing all of the elements in this list in proper sequence (from first to last element). */
// Modification Operations
boolean add(E e); /**Appends the specified element to the end of this list (optional operation) */
boolean remove(Object o); /**Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. */
// Bulk Modification Operations
boolean containsAll(Collection<?> c); /** Returns true if this list contains all of the elements of thespecified collection. */
boolean addAll(Collection<? extends E> c); /**Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (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). */
boolean removeAll(Collection<?> c); /**Removes from this list all of its elements that are contained in the specified collection (optional operation). */
boolean retainAll(Collection<?> c); /**Retains only the elements in this list that are contained in the specified collection (optional operation). */
default void replaceAll(UnaryOperator<E> operator); /**Replaces each element of this list with the result of applying the operator to that element. */
default void sort(Comparator<? super E> c) /**Sorts this list according to the order induced by the specified. */
void clear(); /**Removes all of the elements from this list (optional operation). The list will be empty after this call returns. */
// Comparison and hashing
boolean equals(Object o); /**Compares the specified object with this list for equality. Returns trueif and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are <i>equal</i>.*/
int hashCode(); /** Returns the hash code value for this list. */
// Positional Access Operations
E get(int index); /** Returns the element at the specified position in this list. */
E set(int index, E element); /** Replaces the element at the specified position in this list with the specified element (optional eration). */
void add(int index, E element); /** Inserts the specified element at the specified position in this list (optional operation). */
E remove(int index); /** Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list. */
// Search Operations
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.*/
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.*/
// List Iterators
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. */
// View
List<E> subList(int fromIndex, int toIndex); /** Returns a view of the portion of this list between the specified fromIndex and toIndex. (If fromIndex and toIndex are equal, the returned list is empty.) */
@Override
default Spliterator<E> spliterator(); /** Creates a {@link Spliterator} over the elements in this list. */
}
Example of common Operations of List Interface:
public static void main(String[] args) {
List<String> vowel = new ArrayList<String>();
//add example
vowel.add("A");
vowel.add("I");
//let's insert E between A and I
vowel.add(1, "E");
System.out.println(vowel);
List<String> list = new ArrayList<String>();
list.add("O");
list.add("U");
System.out.println(list);
//appending list elements to letters
vowel.addAll(list); //addAll()
System.out.println(vowel);
//clear example to empty the list
list.clear(); //clear()
System.out.println("after Clear List: " + list);
//size example
System.out.println("letters list size(vowel.size()) = " + vowel.size()); //size()
//set example
vowel.set(2, "E"); //set()
System.out.println(vowel);
//subList example
vowel.clear();System.out.println(vowel);
vowel.add("E");System.out.println(vowel);
vowel.add("E");System.out.println(vowel);
vowel.add("I");System.out.println(vowel);
vowel.add("O");System.out.println(vowel);
vowel.remove("E"); //remove()
System.out.println(vowel);
list = vowel.subList(0, 2); //subList();
System.out.println("letters = " + vowel + ", list = " + list);
vowel.set(0, "A");
System.out.println("letters = " + vowel + ", list = " + list);
list.add("U");
System.out.println("letters = " + vowel + ", list = " + list);
}
Output:
[A, E, I]
[O, U]
[A, E, I, O, U]
after Clear List: []
letters list size(vowel.size()) = 5
[A, E, E, O, U]
[]
[E]
[E, E]
[E, E, I]
[E, E, I, O]
[E, I, O]
letters = [E, I, O], list = [E, I]
letters = [A, I, O], list = [A, I]
letters = [A, I, U, O], list = [A, I, U]
Comments
Post a Comment