Difference between List and ArrayList in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

[Geeks, For, Geeks]

List vs ArrayList in Java

List ArrayList
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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.