AppDividend
Latest Code Tutorials

Java List Interface Example | List Interface In Java Tutorial

0

Java List Interface Example | List Interface In Java Tutorial is today’s topic. The list is an interface in java which extends Collection interface. It is used to store and manipulate items. Insertion and deletion are positional based, and the list can have duplicate items in it. In general, it is used to store lists of objects and preserves the order. List Interface is implemented by ArrayListLinkedListVector, and Stack classes.

Java List Interface

The List interface places additional stipulations, beyond those specified in a Collection interface, on the contracts of an iteratoraddremoveequals, and hashCode methods. 

Declarations for other inherited methods are also included here for the convenience. The ordered collection which is also known as a sequence. A user of this interface has precise control over where in the list, each element is inserted. The user can access items by their integer index (position in the list), and search for elements in the list.

Unlike sets, lists typically allow duplicate elements. More formally, lists usually allow pairs of elements e1 and e2 such that e1.equals(e2), and they usually allow multiple null items if they allow null items at all. It is not that inconceivable that someone might wish to implement the list that prohibits duplicates, by throwing the runtime exceptions when a user attempts to insert them, but we expect this usage to be rare.

For example, the List interface is declared as:

interface List<O>

O specifies the type of the objects that the list will be working with.

Compiled from "List.java"
public interface java.util.List<E> extends java.util.Collection<E> {
  public abstract int size();
  public abstract boolean isEmpty();
  public abstract boolean contains(java.lang.Object);
  public abstract java.util.Iterator<E> iterator();
  public abstract java.lang.Object[] toArray();
  public abstract <T> T[] toArray(T[]);
  public abstract boolean add(E);
  public abstract boolean remove(java.lang.Object);
  public abstract boolean containsAll(java.util.Collection<?>);
  public abstract boolean addAll(java.util.Collection<? extends E>);
  public abstract boolean addAll(int, java.util.Collection<? extends E>);
  public abstract boolean removeAll(java.util.Collection<?>);
  public abstract boolean retainAll(java.util.Collection<?>);
  public void replaceAll(java.util.function.UnaryOperator<E>);
  public void sort(java.util.Comparator<? super E>);
  public abstract void clear();
  public abstract boolean equals(java.lang.Object);
  public abstract int hashCode();
  public abstract E get(int);
  public abstract E set(int, E);
  public abstract void add(int, E);
  public abstract E remove(int);
  public abstract int indexOf(java.lang.Object);
  public abstract int lastIndexOf(java.lang.Object);
  public abstract java.util.ListIterator<E> listIterator();
  public abstract java.util.ListIterator<E> listIterator(int);
  public abstract java.util.List<E> subList(int, int);
  public java.util.Spliterator<E> spliterator();
  public static <E> java.util.List<E> of();
  public static <E> java.util.List<E> of(E);
  public static <E> java.util.List<E> of(E, E);
  public static <E> java.util.List<E> of(E, E, E);
  public static <E> java.util.List<E> of(E, E, E, E);
  public static <E> java.util.List<E> of(E, E, E, E, E);
  public static <E> java.util.List<E> of(E, E, E, E, E, E);
  public static <E> java.util.List<E> of(E, E, E, E, E, E, E);
  public static <E> java.util.List<E> of(E, E, E, E, E, E, E, E);
  public static <E> java.util.List<E> of(E, E, E, E, E, E, E, E, E);
  public static <E> java.util.List<E> of(E, E, E, E, E, E, E, E, E, E);
  public static <E> java.util.List<E> of(E...);
  public static <E> java.util.List<E> copyOf(java.util.Collection<? extends E>);
}

The above text file contains the output of javap command used to extract the java.util.List interface in the terminal. It contains all the methods and constructor contained in the List interface.

 

Java List Interface

#Methods inside List interface

#public abstract int size();

The size() returns the number of elements in this list.

#public abstract boolean isEmpty();

It checks if List is empty or not.

#public abstract boolean contains(java.lang.Object);

It is used to check if the List contains the object mentioned in the parameter.

#public abstract boolean add(E);

It appends the element E at the end of the List.

#public abstract boolean remove(java.lang.Object);

It is used to remove a first occurrence of the element supplied.

#public abstract boolean containsAll(java.util.Collection<?>);

It tells whether the List contains all the elements of the supplied Collection.

#public abstract boolean addAll(java.util.Collection<? extends E>);

It appends all the elements of the supplied Collection to the end of the List.

#public abstract boolean addAll(int, java.util.Collection<? extends E>);

It inserts the elements of the supplied Collection at the given position.

#public abstract boolean removeAll(java.util.Collection<?>);

It removes all the items from the List that are inside that Collection.

#public abstract boolean retainAll(java.util.Collection<?>);

It retains the elements in the List that are inside the Collection.

#public void replaceAll(java.util.function.UnaryOperator<E>);

It replaces each element of this list with the result of applying the supplied operator to that element.

#public void sort(java.util.Comparator<? super E>);

It sorts using the supplied parameters.

#public abstract void clear();

It clears all the elements of the List.

#public abstract boolean equals(java.lang.Object);

It compares the supplied object with the List.

#public abstract int hashCode();

It returns hash code of the List.

#public abstract E get(int);

It returns the element at the specified position.

#public abstract E set(int, E);

It replaces the element of the given index with the supplied element.

#public abstract void add(int, E);

It inserts the supplied element at the supplied position.

#public abstract E remove(int);

It removes the element from the List using the supplied position. 

#public abstract int indexOf(java.lang.Object);

It returns an index of a first occurrence of the specified element, or -1 if the List does not contain the element.

#public abstract int lastIndexOf(java.lang.Object);

It returns the last occurrence of the specified element, or -1 if the List does not contain the element.

#public abstract java.util.ListIterator<E> listIterator();

It returns the list iterator over the elements in this list.

#public abstract java.util.ListIterator<E> listIterator(int);

It returns the list iterator over the elements in this list, starting at the specified position in the List.

#public abstract java.util.List<E> subList(int, int);

It returns the view of the portion of this List between the specified first parameter(inclusive) and the second parameter(exclusive).

#public java.util.Spliterator<E> spliterator();

It is used to create the spliterator over the elements in a list.

#public abstract java.lang.Object[] toArray();

It returns the array containing all the elements in this list in the correct order.

#public abstract <T> T[] toArray(T[]);

It returns the array containing all the elements in this list in the correct order.

#Program demonstrating List using ArrayList

See the following program.

import java.util.*;

class Example1 {
  public static void main(String[] args) {
    List<Integer> myList = new ArrayList<Integer>(); // Using ArrayList to make Integer List
    myList.add(1); // Insertion of 1 at position 0
    myList.add(2); // Insertion of 2 at position 1
    myList.add(3); // Insertion of 3 at position 2
    myList.remove(1); // Removal of element at position 1
    for (Integer l : myList) {
      System.out.println(l);
    }

  }
}

See the following output.

 

Java List Interface Example

#Program demonstrating List using LinkedList

See the following List using the LinkedList program.

import java.util.*;

class Example2 {
  public static void main(String[] args) {
    List<String> myList = new LinkedList<String>(); // Using LinkedList to make List of String
    myList.add("Papa "); // Insertion at position 0
    myList.add("John "); // Insertion at position 1
    myList.add("Loves "); // Insertion at position 2
    myList.add("Pizza "); // Insertion at position 3
    myList.add(1, "df"); // Insertion at position 1
    myList.remove(1); // Removal of element at position 1
    for (String s : myList) {
      System.out.print(s);
    }

    System.out.println();
  }
}

See the following output.

 

Program demonstrating List using LinkedList

#Program demonstrating List using Stack

See the following code.

import java.util.*;

public class Example3 {
  public static void main(String[] args) {
    List<String> myList = new Stack<String>(); // Using Stack to make List of String
    myList.add("Papa "); // Insertion at position 0
    myList.add("John "); // Insertion at position 1
    myList.add("Loves "); // Insertion at position 2
    myList.add("Pizza "); // Insertion at position 3
    myList.add(1, "df"); // Insertion at position 1
    myList.remove(1); // Removal of element at position 1
    for (String s : myList) {
      System.out.print(s);
    }
    System.out.println();
  }
}

See the following output.

 

Program demonstrating List using Stack

Finally, Java List Interface Example | List Interface In Java Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.