List Interface in Java

In this module, we are going to discuss List Interface in Java Programming with example. In Java Programming List is an ordered collection. The List Interface is found in java. util package and implements the collection interface. The implementation classes for List Interfaces are ArrayList, LinkedList, Stack, Vector.

The list is an interface we can not create an object. The following is the syntax for creating List Interface.

Syntax:

List<Datatype> list_var=new ArrayList<>();
List<Datatype> list_var=new LinkedList<>();
List<Datatype> list_var=new Vector<>();
List<Datatypr> list_var=new Stack()<>();

The above statements are for creating List and implementing List with different interfaces.

Methods Used by List:

  • add(element): used for adding elements to list at last position.
  • add(index, element): used for adding elements to list at the passed index position.
  • size(): return size of list.
  • remove(element): removes the first occurrence of the element from the list.
  • remove(index): removes the element at a given index.
  • set(position,element): replaces the index position element with new passed element.
  • get(index): returns the element positioned at the passed index.
  • contains(element): returns boolean value by checking element present in the list or not.
  • clear(): removes all elements from the list.

Implementation of List Interface in Java

The following code gives a complete description of List Interface.

import java.io.*;
import java.util.*;
public class listimpl
{
    public static void main(String[] args)
    {
        List<String> list=new ArrayList<>();    //creation of list with var list
        list.add("deepak");
        list.add("seshu");                      //add elements to list
        list.add("nikilesh"); 
        list.add(2,"tarun");                    //add elements to list based on index
        list.add(1,"vishnu");
        System.out.println(list.size());        //printing the size of list
        System.out.println(list);
        list.remove("tarun");                   //removing element from list
        System.out.println(list);
        list.remove(3);                         //removing element based on index
        System.out.println(list);
        System.out.println(list.contains("tarun")+" "+list.contains("vishnu"));    //checks if element present in list
        list.set(1,"nikilesh");                 //replace the element at index 1 with new element
        System.out.println(list);
        System.out.println(list.get(0));        //getting elements from list based on index
        list.add("mani");
        Collections.sort(list);                 //collections which sorts all elements in ascending order in list
        System.out.println(list);
    }
}

Output:

5
[deepak, vishnu, seshu, tarun, nikilesh]
[deepak, vishnu, seshu, nikilesh]
[deepak, vishnu, seshu]
false true
[deepak, nikilesh, seshu]
deepak
[deepak, mani, nikilesh, seshu]

Explanation:

Here, we have implemented all the defined methods for the List. These are the most commonly used methods on the list by programmers.

Reference:

https://docs.oracle.com/javase/7/docs/api/java/util/List.html

 

Leave a Reply

Your email address will not be published. Required fields are marked *