Difference between list and arraylist in Java
In this post, you will learn about the difference between list and arraylist of the Java programming language.
The Java platform includes a collections framework. Java Collection framework provides many interfaces(List) and classes(ArrayList, Vector, LinkedList).
List in Java
A list is an ordered collection. It contains the index-based methods to insert, update, delete, and search the elements. We can access elements by their index and also search for elements in the list. A list can contain duplicate values and null elements. List interface gives positional access and insertion of elements and preserves the insertion order. The list interface extends Collection and declares the behaviour of a collection that stores a sequence of elements.
List Interface declaration
public interface List<E> extends Collection<E>
ArrayList in Java
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate element of different data types. ArrayList is a part of collection framework and is present in java.util package. It is like an array, but there is no size limit. ArrayList is a dynamic array, and we do not have to specify the size while creating it. We can add or remove elements at anytime. ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time. It provides some useful methods like add(), addAll(), capacity(), clear(), clone(), elements(), equals(), size(), set(), remove(), trimToSize().
ArrayList Declaration
ArrayList<Type> arrayList= new ArrayList<>();
Difference between list and arraylist in Java
There are many differences between list and arraylist. Here, we have mentioned most of them-
List | ArrayList |
List is an interface. | ArrayList is a class. |
List cannot be instantiated. | ArrayList can be instantiated. |
List interface extends Collection Framework. | ArrayList extends AbstractList and implements List Interface. |
We can use the List for creating a list of various objects which are associated with their index numbers. | We can use the ArrayList to create a dynamic array that contains objects. |
Example of List in Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
// Creating a list
List list1 = new ArrayList();
//add two values to the list
list1.add(0, 31);
list1.add(1, 67);
System.out.println("The initial List:\n" + list1);
// Creating another list
List list2 = new ArrayList();
list2.add(32);
list2.add(89);
list2.add(78);
// add list cp_list to intList from index 2
list1.addAll(2, list2);
System.out.println("List after adding another list:\n"+ list1);
// Removes element from index 0
list1.remove(0);
System.out.println("List after removing element:\n" + list1);
// Replace value of last element
list1.set(3, 60);
System.out.println("List after replacing the value of last element:\n" + list1);
}
}
Output of the above code:
The initial List:
[31, 67]
List after adding another list:
[31, 67, 32, 89, 78]
List after removing element:
[67, 32, 89, 78]
List after replacing the value of last element:
[67, 32, 89, 60]
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java