List interface in Java with ArrayList, LinkedList and Vector Class
List Interface (java.util.List) :
List is an ordered collection. It is also known as sequence.
List is child interface of Collection framework. List comes under java.util package.
We can access any element by their index and search for element also in the List. List maintains ordered elements and duplicate also.
Methods of List Interface :
- add()
- addAll()
- clear()
- contains()
- containsAll()
- equals()
- get()
- hashCode()
- indexOf()
- isEmpty()
- iterator()
- lastIndexOf()
- listIterator()
- remove()
- removeAll()
- retainAll()
- set()
- size()
- subList()
- toArray()
For use functionality of List interface we can use following classes :
- ArrayList
- LinkedList
- Vector
- Stack
ArrayList Class :
We can say, ArrayList is better version of Array. It is resizable array. means as simple array, we does not have to specify size of array.
The Java ArrayList class can store a group of many objects. We can not store primitives (int, double, float, etc) in ArrayList only Objects (Integer, Float, etc) are allowed.
ArrayList class is same as Vector class. the only difference is ArrayList is non-synchronized and Vector is synchronized.
How to create new ArrayList?
ArrayList list = new ArrayList();
OR
List list = new ArrayList();
It will create new empty arraylist.
Lets jump on code for some operations we can perform on ArrayList.
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(4);
list.add(2);
list.add(90);
list.add(9);
list.add(5);
System.out.println("Get elements by specific index : "+ list.get(1));
System.out.println("Get index of specific element : "+ list.indexOf(1));
System.out.println("Check if list is empty : "+ list.isEmpty());
System.out.println("Check if list contains specific vlaue : "+ list.contains(2));
System.out.println("Check if list is empty : "+ list.isEmpty());
System.out.println("Replace element at specific index : "+list.set(1, 10));
System.out.println("Remove an element at specific index : "+ list.remove(0));
System.out.println("Iterate through arraylist : ");
list.iterator().forEachRemaining(System.out::println);
}
}
Output :
Get elements by specific index : 4
Get index of specific element : 0
Check if list is empty : false
Check if list contains specific vlaue : true
Check if list is empty : false
Replace element at specific index : 4
Remove an element at specific index : 1
Iterate through arraylist :
10
2
90
9
5
LinkedList Class :
An ArrayList is an index based data structure backed by an Array. On the other hand, a LinkedList stores its data as a list of elements and every element is linked to its previous and next element.
LinkedList is also not synchronized.
In LinkedList every element is Node, which keeps a reference to the next and previous one.
LinkedList class also implemets Deque interface, so we can use Queue implementation also.
When to use LinkedList over ArrayList?
When insertion and deletion operations are used frequently, LinkedList is better choice.
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(10);
list.add(20);
list.add(20);
list.add(90);
list.add(50);
list.add(30);
System.out.println("Size of list : "+list.size());
//Adding element at First
list.addFirst(60);
//Adding element at last
list.addLast(100);
System.out.println("Getting index of last element : "+list.indexOf(100));
list.iterator().forEachRemaining(System.out::println);
//Removing first element
list.removeFirst();
//Removing last element
list.removeLast();
System.out.println("List size : "+list.size());
}
}
Output :
Size of list : 6
Getting index of last element : 7
60
10
20
20
90
50
30
100
List size : 6
Types of Linked List :
There is Three types of Linked List in java.
- Singly linked list (Uni-directional)
- Doubly linked list (Bi-directional)
- Circular linked list
- Reverse Linked List (LL) using Iterative (While loop) and Recursive approach in Java
- Merging two sorted Linked List using Recursion approach with Stack trace
Vector Class :
Using Vector class, we can create resizable array same as Arraylist class. but there is some differences exist.
Vector class is synchronized, means it is thread safe. If one Thread is accessing vector then another Thread have to wait until previous one complete the operation on Vector.
import java.util.Vector;
public class LinkedListDemo {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
vector.add("Java");
vector.add("Python");
vector.add("JavaScript");
vector.add("Dot Net");
vector.add("Php");
System.out.println("Getting element by index : "+ vector.get(1));
System.out.println("Set element on specific index : "+ vector.set(3, "CSharp"));
System.out.println("Remove element by index : "+ vector.remove(4));
System.out.println(vector);
}
}
Output :
Getting element by index : Python
Set element on specific index : Dot Net
Remove element by index : Php
[Java, Python, JavaScript, CSharp]
Stack Class :
Stack class is not implementing List interface directly but it extends Vector class and can use functionality of List interface.
Stack follows LIFO (Last in First out) order. means element add top of stack and removed from top only.
import java.util.Stack;
public class LinkedListDemo {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
stack.push("Java");
stack.push("Python");
stack.push("JavaScript");
stack.push("Dot Net");
stack.push("Php");
System.out.println("Getting top element of Stack without remove : "+ stack.peek());
System.out.println("Pop element form Stack : "+ stack.pop());
System.out.println("Checking stack is empty or not : "+ stack.empty());
System.out.println(stack);
}
}
Output :
Getting top element of Stack without remove : Php
Pop element form Stack : Php
Checking stack is empty or not : false
[Java, Python, JavaScript, Dot Net]
Happy learning. Happy coding.
Comments
Post a Comment