Table of contents
1.
Introduction
2.
What is a List in Java?
2.1.
Order of Insertion
2.2.
Duplicates
2.3.
Index-based Access
2.4.
Code
2.5.
Java
2.6.
Output
3.
What is an ArrayList in Java?
3.1.
Dynamic Size
3.2.
Order of Insertion
3.3.
Index-based Access
3.4.
Duplicates
3.5.
Code
3.6.
Java
3.7.
Output
4.
Difference between List and ArrayList in Java
5.
Frequently Asked Questions
5.1.
Is it possible to have dynamic sized ArrayList and List in Java?
5.2.
In which situations List is preferred over ArrayList?
5.3.
What are some other implementations of the List interface in Java?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between List and ArrayList in Java

Author Aayush Sharma
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the programming world, Java is a very versatile and widely used programming language. Java is a platform-independent and object-oriented language. As a result of these features, Java is widely used in places like web applications, mobile applications, game development etc. Lists and ArrayList are very commonly used interfaces in Java. 

Difference Between List and ArrayList in Java

This blog will discuss the List and ArrayList interfaces in Java in detail. We will also talk about the differences between List and ArrayList in Java. Ultimately, we will conclude by looking at some frequently asked questions.

What is a List in Java?

List is an interface that represents the collection of elements in Java. The List is a part of the fundamental part of the Java collections Framework. It has a collection of useful classes and interfaces for data collection.

Some of the key features of the List are described below.

Order of Insertion

In a List, the elements are ordered based on their insertion order. In other words, the part inserted at the beginning will be at the index 0, the aspect, and so on.

Duplicates

The List allows us to store duplicate elements in them. We can keep multiple elements with the same values in a list.

Index-based Access

The elements present in the list can be accessed by their index. For example, if we want to access the first element in a list we can get the value at the index 0 (due to 0 based indexing) and so on.

Let us understand the working of Lists in Java with the help of an example.

Code

  • Java

Java

import java.util.ArrayList;
import java.util.List;

public class ListExample {
public static void main(String[] args) {

// Creating a List of numbers
List<Integer> numbers = new ArrayList<>();

// Adding elements to the List
numbers.add(0);
numbers.add(1);
numbers.add(2);

// Accessing elements by index
int firstNumber=numbers.get(0);
int secondNumber=numbers.get(1);

// Iteration of the list
for (Integer number : numbers) {
System.out.println(number);
}
}
}
You can also try this code with Online Java Compiler
Run Code

Output

Output

What is an ArrayList in Java?

ArrayList is a dynamic array-based implementation of the List interface. The Java Collections framework provides the ArrayList implementation of List. It is a very commonly used data structure in Java because of its useful features.

Some of the most useful features of ArrayList are described below.

Dynamic Size

ArrayList can automatically handle the resizing of the array if needed by the user. This means that we can remove or add elements to the array, and it automatically handles the resizing of the ArrayList.

Order of Insertion

In an ArrayList, the elements are ordered based on their insertion order. In other words, the element inserted at the beginning will be at the index 0, and the element.

Index-based Access

The elements present in the ArrayList can be accessed by their index. For example, if we want to access the first element in an ArrayList, we can get the value at the index 0 (due to 0-based indexing), and so on.

Duplicates

ArrayList also allows the user to store duplicate elements in it. Hence, we can add multiple instances of the same element, but they will be counted as different.

Let us understand the working of Lists in Java with the help of an example.

Code

  • Java

Java

import java.util.ArrayList;
import java.util.List;

public class ListExample {
public static void main(String[] args) {

// Creating a List of numbers
ArrayList<Integer> numbers = new ArrayList<>();

// Adding elements to the List
numbers.add(0);
numbers.add(1);
numbers.add(2);

// Accessing elements by index
int firstNumber=numbers.get(0);
int secondNumber=numbers.get(1);

// Iteration of the list
for (Integer number : numbers) {
System.out.println(number);
}
}
}
You can also try this code with Online Java Compiler
Run Code

Output

Output

Difference between List and ArrayList in Java

So, now that we know about both List and ArrayList in Java, let us look at some of the key differences between them in a tabular format.

List

ArrayList

The List is an interface in the Java language. ArrayList is the implementation of the List class.
The List is not inherently dynamically sized. ArrayList is dynamically sized.
The performance of the List depends on the implementation of the List. ArrayList provides fast access due to the presence of random iterators.
We can not create List objects directly. ArrayList objects can be directly created.
The List is more generic. ArrayList is comparatively more specific.
The List is suitable in scenarios where we want to have more flexibility. ArrayList is useful when we want to have a more particular dynamic size array.
List cannot be instantiated. ArrayList can be instantiated.
List consumes less memory for small collections. ArrayList consumes more memory because it reserves space for some extra elements.
List only provides a basic operator for traversing elements. ArrayList provides both Iterator and ListIterator for traversing elements.

 

Frequently Asked Questions

Is it possible to have dynamic sized ArrayList and List in Java?

No, we cannot have both List and ArrayList having dynamic sizes. The List is an interface in Java, and hence, its size cannot be dynamic. On the other hand, ArrayList is the dynamic version of the List; hence, it can resize itself.

In which situations List is preferred over ArrayList?

The List is a more generic form of ArrayList in Java. ArrayList is a specific class derived from the List interface. We can use List in scenarios where we want a generic and more flexible code abstraction.

What are some other implementations of the List interface in Java?

The List interface in Java provides various implementations other than List. These include the LinkedList class, vector class, CopyOnWriteArrayList class, etc. Each of these classes has its characteristics and use cases.

Conclusion

In this article, we discussed the difference Between List and ArrayList in Java. We discussed in detail about List and ArrayList in Java. We also looked at the significant differences between List and ArrayList in Java. Ultimately, we concluded by discussing frequently asked questions about List and ArrayList and their significant differences.

So now that you know about Difference between List and ArrayList in Java, you can refer to similar articles.
 

You can also consider our paid courses such as DSA in Java to give your career an edge over others!

Happy Learning!

Live masterclass