Java contains the “Collection” framework which includes multiple classes and interfaces for representing a group of objects. This collection enables storing the group of objects as a single unit. This interface contains the “List” interface and the “ArrayList” class which can be used to perform multiple operations such as sorting, insertion, deletion, and manipulation of elements as per the requirements.
Contents Overview
- What is a List in Java?
- Why Do We Need Lists in Java?
- What is an ArrayList in Java?
- Why Do We Need ArrayList in Java?
- Core Differences Between List and ArrayList in Java
What is a List in Java?
“List” corresponds to the child interface of the “Collection” framework which is an ordered objects collection in which the duplicate values can also be contained. This interface is contained in the “java.util” package and can implement the following classes:
- “ArrayList” Class.
- “LinkedList” Class.
- “Stack” Class.
- “Vector” Class.
The “List” interface is also a base of the “ListIterator” classes using which the list can be iterated from forward as well as backward.
Syntax
The list interface can be instantiated with the discussed classes via the following syntaxes:
List <dt> list= new ArrayList();
List <dt> list = new LinkedList();
List <dt> list = new Vector();
List <dt> list = new Stack();
In the above syntaxes, “dt” refers to the data type of the list.
Why Do We Need Lists in Java?
“Lists” in Java enable the inclusion of the “duplicate” and “null” elements and the elements can be fetched from the lists conveniently via indexing. Moreover, its compatibility with multiple classes makes it a favorable choice for developers.
Example 1: Using “List” Interface With ArrayList Class in Java
The following code example uses the “List” interface with the “ArrayList” class to add and remove the values using this class:
package jbArticles;
import java.util.*;
public class ListArrayList {
public static void main(String[] args) {
List<String> items = new ArrayList<String>();
items.add("This");
items.add("is");
items.add("Java");
System.out.println("List Values -> " +items);
items.remove(0);
System.out.println("Updated List Values After Removal-> " +items);
}}
Code Explanation
- First of all, import the “java.util.*” package to access all the classes within the “java.util” package.
- Create an instance with reference to the ArrayList class of the “String” type.
- Insert the given string values in the ArrayList via the “add()” method and display them.
- After that, apply the “remove()” method having the stated index to remove the corresponding value at the particular index from the list.
Output
In this output, it is evident that the values are added and removed appropriately in the List.
Example 2: Using “List” Interface with LinkedList Class in Java
This demonstration uses the “List” interface with the “LinkedList” class such that the integer values are added and removed from the list:
package jbArticles;
import java.util.*;
public class ListArrayList {
public static void main(String[] args) {
List<Integer> items = new LinkedList<Integer>();
items.add(1);
items.add(2);
items.add(3);
System.out.println("List Values -> " +items);
items.remove(0);
System.out.println("Updated List Values After Removal-> " +items);
}}
Code Explanation
- Likewise, create a List instance representing the “LinkedList” class in which the integer values are to be added and removed.
- Add the specified integer values in the list using the “add()” method.
- Lastly, similarly, remove the indexed value i.e., first from the list.
Output
Bonus Tip: Specifying the class i.e., “LinkedList” does not ensure making use of its methods i.e., “push()”, “pop()” etc as long as it is specified in the following way:
LinkedList<Integer> items = new LinkedList<Integer>();
The following demonstration makes use of the “Vector” and “Stack” classes with the “List” interface returning the added values and removing them, respectively:
What is an ArrayList in Java?
“ArrayList” is a part of the “Collection” framework contained in the “java.util” package that utilizes a dynamic array for containing/storing the elements and there is no size limit in it.
This class implements the “List” interface. Like “List”, the ArrayList’s size increases automatically if the collection grows or shrinks. Moreover, “ArrayList” also enables randomly invoking the list and cannot be used for primitive data types such as “int”, “char” etc.
Syntax
ArrayList obj = new ArrayList();
The above syntax makes a new memory in the heap memory by creating an instance of the ArrayList where “new” represents the keyword and “ArrayList()” is the constructor.
After passing the identifiers in the above syntax, the syntax looks like the following:
ArrayList<Integer> obj = new ArrayList<>();
ArrayList<String> obj = new ArrayList<>();
ArrayList<Object> obj = new ArrayList<>();
The “Integer”, “String”, and “Object” indicate the data types of ArrayList. The “Object” type, however, is compatible with both the “Integer” and “String” data types.
Why Do We Need ArrayList in Java?
“ArrayList” is a resizable array such that its size is flexible i.e., can be changed dynamically. It is such that the elements can be appended and removed from it as per the requirements, thereby helping the programmer in managing the memory efficiently.
Example: Using “ArrayList” in Java
In this demonstration, the use of “ArrayList” will be explained via the following code:
package jbArticles;
import java.util.*;
public class ListArrayList {
public static void main(String[] args) {
ArrayList<String> items = new ArrayList<String>();
items.add("This");
items.add("is");
items.add("Java");
System.out.println("ArrayList Values -> " +items);
items.remove(0);
System.out.println("Updated ArrayList Values After Removal-> " +items);
}}
Code Explanation
- Create an ArrayList instance of the “String” type, add the specified string values in it using the “add()” method, and display it.
- Now, remove the target value from the ArrayList by referring to its index.
Output
From this output, it can be observed that the values are added and removed from the ArrayList accordingly.
In this case, since the classes other than “ArrayList” are not compatible with ArrayList, specifying any other class returns an error, demonstrated below:
Core Differences Between List and ArrayList in Java
List | ArrayList |
---|---|
It is an interface. | It is a class. |
It extends the “Collection” framework. | It extends the “AbstractList” class and implements the “List” interface. |
It manipulates the object instantly. | It manipulates the objects slowly as compared to List. |
It cannot be instantiated. | It can be instantiated. |
It creates an elements list that is associated with indexes. | It creates a dynamic array that comprises objects. |
The “List” interface cannot be expanded once created. | It creates an object array where the array can grow/expand dynamically. |
Conclusion
The “List” corresponds to an interface that cannot be expanded once created and cannot be instantiated as well whereas “ArrayList” is a class that can be instantiated and creates an array of objects where the array can grow dynamically. Both of these Java functionalities are contained in the “java.util” package. However, it is recommended to use “List” while performing polymorphism.