Difference between arraylist and linkedlist
In this post, you will learn about the difference between the arraylist and linkedlist.
The Java platform includes a collections framework. Java Collection framework provides many interfaces(List) and classes(ArrayList, Vector, LinkedList).
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<>();
ArrayList Program 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]
Linkedlist in Java
The LinkedList class is a collection which can contain many objects of the same type. Java linkedList class can contain duplicate elements. It is non synchronized. Java linkedList class can be used as a list, stack or queue.
LinkedList class declaration
public class LinkedList<E> extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
LinkedList Program in Java
import java.util.*;
public class LinkedListProgram{
public static void main(String args[]){
LinkedList llist=new LinkedList();
llist.add("Smith");
llist.add("Joy");
llist.add("Parul");
llist.add("Den");
Iterator itr=llist.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output of the above code:
Smith
Joy
Parul
Den
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