The list and ArrayList in Java, both are data structures that are widely used to code different applications. Java contains various methods and functions that belong to the List and ArrayList. While both the data structure seems to look quite similar there are differences between the two.

In this article, we will discuss the following content in detail about the List and ArrayList in Java:

  • What is a List in Java?
  • What is ArrayList in Java?
  • Difference between the List and ArrayList in Java
  • Relationship between the List and ArrayList in Java
  • Conclusion

What is a List in Java?

The list in Java is an interface that basically extends the collection interface. It represents an ordered collection of data and uses the index to insert, update, and retrieve the elements. The list in Java has the capability to store the null values also. For a better understanding let us go through the syntax,

public interface List<E> extends Collection<E>

In the above syntax,

  • The list is an interface.
  • The list interface extends the collection interface.

Let us look at the code example below to ensure a better understanding of the list in Java.

import java.util.LinkedList;
import java.util.List;
 
public class ListExample1 {
    public static void main(String a[]){  
        List<String> newlist = new LinkedList<String>();
        newlist.add("Print");
        newlist.add("Hello");
        newlist.add("in");
        newlist.add("Java");
        //print the list
        System.out.println("The Output is: "+newlist);   
    }
}

In the above code block:

  • The LinkedList and List packages are imported.
  • The class is created/defined as “ListExample1”.
  • The object for the list is created as “newlist” and the elements are added to the list using the created object.
  • The outcome will be printed/displayed in the form of a list.

Output

What is an ArrayList in Java?

The ArrayList in Java are the resizeable arrays that are considered dynamic in nature. The ArrayList can have as many elements present in the list without declaring the size as compared to the list.

The way the ArrayList is created along with the code example is illustrated below.

ArrayList<String> object =new ArrayList<String>()

In the above declaration, 

  • The String or Integer is declared
  • The object for the ArrayList is created to perform different operations.
import java.util.ArrayList;
import java.util.List;
 
public class ListExample2 {
    public static void main(String a[]){
       
        ArrayList<String> newlist = new ArrayList<String>();
        newlist.add("This");
        newlist.add("is");
        newlist.add("Java");
        newlist.add("Version 8");
        //print the list
        System.out.println("The Output is: "+newlist);     
    }
}

Here:

  • The List and ArrayList classes are imported from the “java.util”.
  • The class is created/declared as “ListExample2”.
  • The add() method inserts the elements into the ArrayList.
  • The list is printed/displayed to the output console.

Output

In the output stated below, the list is printed with the elements declared in the input of the ArrayList.

Java List Vs. ArrayList – What’s the Difference?

The key differences between the List and the ArrayList in Java are depicted in the table below.

S.NoListArrayList
1.List in Java is defined as an InterfaceThe ArrayList in Java is a class.
2.The list can not be instantiated in JavaArrayList can be instantiated in Java
3.The elements in the List are based on the indexThe ArrayList is referred to as a dynamic array that contains objects.
4.It extends the collection framework in JavaIt extends the abstract List class and implements the List interface of Java.
5.The search operation is slower in List.The search operation is faster in ArrayList.
6.Insertion and deletion are faster in List.Insertion and deletion are slower as compared to List.
7.The namespace is System.Collections.GenericThe namespace for ArrayList is System.Collections

Relationship Between Java List and ArrayList 

List is an interface, while the ArrayList is not a list itself instead it is a class that implements/inherits the List interface. The code below shows the connection between the list and ArrayList in Java.

import java.util.ArrayList;
import java.util.List;
public class ListExample1 {
    public static void main(String a[]){
        //Add the elements
        List<String> newlist = new ArrayList<String>();
        newlist.add("Java");
        newlist.add("Ruby");
        newlist.add("Python");
        //print the list
        System.out.println("The Output is: "+newlist);
        //create an ArrayList               
    }
}

In the above code block:

  • The required packages are imported in Java.
  • The add() method adds/inserts the elements to the list in Java.
  • The resultant value of the code is displayed/printed using the println().

Output

This concludes the “List Vs ArrayList” debate in Java.

Conclusion

The list in Java belongs to the collection framework and the Arraylist implements the list interface in Java. The main difference occurs since the list is static in nature and the ArrayList is dynamic since the elements in the ArrayList can be modified and updated. In this article, we have shown the working of the List and ArrayList in Java along with the differences between both.