Skip to main content

List Interface of JAVA 8

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

Popular posts from this blog

A brief history of java

A brief history of java: James Gosling The history of Java Language is more interesting. The history of Java starts from “Green Project”. In 1991 The Green Project initiated by James Gosling , Mike Sheridan , and Patrick Naughton . A small group of Sun engineers called the "Green Team" believed that the next wave in computing was the union of digital consumer devices and computers. Led by James Gosling, the team worked around the clock and created the programming language that would revolutionize our world – Java.   The language was initially called Oak ( Oak is a symbol of strength and chosen as a national tree of many countries like U.S.A., France, Germany, Romania etc ). Later the project went by the name Green and was finally renamed Java , from Java coffee (produced in the Java, is an island of Indonesia).  Sun Microsystem s released the first public implementation as Java 1.0 in 1995. It promised "Write Once, Run Anywhere" (WORA), pr

Features of Java:

Features of Java: In   Java Programming language we introduce with some keyword like Object-oriented, Simple, Portable Platform independent   etc.   Yes! These are feature of Java. There are many features in Java. They are also known as java buzzwords. The main features of Java are: Simple Object-Oriented   Portable   Platform independent     Secured     Robust   Architecture-neutral   Dynamic     Interpreted     High Performance     Multithreaded     Distributed The Java Features are given below in detail. Simple Java is easy to learn and its syntax is quite simple, clean and easy to understand. The confusing and ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner way. No need to remove unreferenced objects because there is Automatic Garbage Collection in java. E.g.: Pointers and Operator Overloading are not there in java but were an important part of C++.   Object-oriented

What is Java?

What is Java? The first released of Java on Jan 23, 1996 by Sun Microsystems. Java is a computer programming language. It is a widely used programming language. It is the most popular programming language for Android Smartphone. It’s known as a high-level language because it can be read and written easily by humans. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute. It enables programmers to write computer instructions using English-based commands instead of having to write in numeric codes. Java was designed to have the look and feel of the  C++  language, but it is simpler to use than C++ and enforces an  object-oriented programming  model. Java can be used to create complete applications that may run on a single computer or be distributed among servers and clients in a network. It can also be used to build a small application module or applet for use as part of a webpage.