Skip to content
Related Articles

Related Articles

Improve Article

Difference between List and ArrayList in Java

  • Last Updated : 02 Jul, 2020

A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit This framework consists the List Interface as well as the ArrayList class. In this article, the difference between the List and ArrayList is discussed.

Difference-between-List-and-ArrayList-in-Java

List: The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List interface is implemented by the classes of ArrayList, LinkedList, Vector and Stack. List is an interface, and the instances of List can be created by implementing various classes. The following is an example to demonstrate the implementation of a list:




// Java program to demonstrate the
// working of a List with ArrayList
// class
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Type safe list, stores only string
        List<String> al = new ArrayList<String>();
        al.add("Geeks");
        al.add("for");
        al.add("Geeks");
  
        System.out.println(al);
    }
}

Output:

[Geeks, For, Geeks]

ArrayList: ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. This class implements the List interface. Similar to a List, the size of the ArrayList is increased automatically if the collection grows or shrinks if the objects are removed from the collection. Java ArrayList allows us to randomly access the list. ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases. The following is an example to demonstrate the implementation of an ArrayList:




// Java program to demonstrate the
// working of an ArrayList class
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Type safe ArrayList, stores
        // only string
        ArrayList<String> al
            = new ArrayList<String>();
  
        al.add("Geeks");
        al.add("for");
        al.add("Geeks");
  
        System.out.println(al);
    }
}

Output:

[Geeks, For, Geeks]

List vs ArrayList in Java

ListArrayList
List is an Interface.ArrayList is a Class.
List interface extends the Collection framework.ArrayList extends AbstractList class and implements List interface.
List cannot be instantiated.ArrayList can be instantiated.
List interface is used to create a list of elements(objects) which are associated with their index numbers.ArrayList class is used to create a dynamic array that contains objects.
List interface creates a collection of elements that are stored in a sequence and they are identified and accessed using the index.ArrayList creates an array of objects where the array can grow dynamically.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :