Recent Tutorials and Articles
    Introduction to List in Java
    Published on: 7th September 2013

    This tutorial introduces List interface and it's implementation classes such as ArrayList and LinkedList along with explaining their special properties.

    Since this tutorial is using Generics so it is expected that reader has got decent knowledge of Generics. If you are new to Generics, you are recommended to read this tutorial first - Introduction to Generics

    List interface in Java is an ordered Collection (it extends Collection interface) and has got following specifications -

    1. Maintains Insertion Order i.e. elements are removed in the same order as they are inserted in
    2. May contain duplicates
    3. Provides positional access i.e. elements can be retrieved by specifying their index in List

    Apart from above specifications, List interface includes the methods for following operations -

    1. Positional access
    2. Search
    3. Iteration
    4. Range view

    To see all the methods of List interface, you may visit Oracle List interface specification here.

    As we know List is an interface and we obviously need to have its implementation in order to use its features. Following are 2 basic implementations of List that Java collections provide us with -

    1. ArrayList
      1. Based on Array hence provide fast index based access
      2. inserting an element between 2 elements takes O(n) time
      3. Permits null
      4. Not synchronized i.e. should not be used in scenario where multiple threads could be accessing it simultaneously
    2. LinkedList
      1. Index based access is not efficient
      2. Inserting and deleting an element is very efficient as it takes O(1) time
      3. Permits null
      4. Not synchronized i.e. should not be used in scenario where multiple threads could be accessing it simultaneously

    One thing worth noting is that if lists are used using List interface reference, it's very easy to switch from ArrayList to LinkedList and vice-versa, as we will see in below examples.

    ArrayListDemo.java
    package com.sts.allprogrammingtutorials.collections;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class ArrayListDemo {
    
    	public static void main(String[] args) {
    		//Declaring a list using List interface and creating an instance of ArrayList
    		final List<String> strings = new ArrayList<String>();
    		
    		//adding an element to a list
    		strings.add("First String Element 1");
    		
    		//getting an element using its index
    		System.out.println("Element retrieved at index - 0 : " + strings.get(0));
    
    		//setting an element at an index
    		strings.set(0, "Updated String Element 1");
    		
    		//getting an element using its index
    		System.out.println("Element retrieved after updating element at index -0 : " + strings.get(0));
    		
    		//Inserting a new element at index 0
    		strings.add(0, "New First String Element 1");
    		
    		//getting an element using its index
    		System.out.println("Element retrieved after inserting element at index -0 : " + strings.get(0));
    		
    		//Removing an element at index 0
    		strings.remove(0);
    		
    		//getting an element using its index
    		System.out.println("Element retrieved after deleting element at index -0 : " + strings.get(0));
    		
    		//Checking whether list has got any elements
    		System.out.println("Is List empty: " + strings.isEmpty());
    	}
    }
    
    
    Output:
    Element retrieved at index - 0 : First String Element 1
    Element retrieved after updating element at index -0 : Updated String Element 1
    Element retrieved after inserting element at index -0 : New First String Element 1
    Element retrieved after deleting element at index -0 : Updated String Element 1
    Is List empty: false
    
    

    As you noticed that we are using List interface reference to interact with ArrayList. This makes it easier to move from one list implementation to another. So if you would like to move above code from ArrayList to LinkedList, we just need to replace ArrayList with LinkedList in first line as below -

    //Declaring a list using List interface and creating an instance of LinkedList
    final List<String> strings = new LinkedList<String>();
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 7th September 2013

    Comment Form is loading comments...