List
- List is an interface which has Collection as a parent interface.
- List allows duplicate elements.
- List is indexed bases.
- There are multiple implemented classes for list.
ArrayList
- ArrayList implements the property from List.
- Can Store values dynamically in array list.
- Can Store different type of values inside array list.
- ArrayList is backed by array Data Structure internally.
- In arraylist every value is store into a separate index.
- Array list maintain the insertion order.
- ArrayList allows us random access.
- ArrayList provide slower performance for removal and addition (modification) operation.
- ArrayList provides faster performance for iteration/retrieval operation.
- ArrayList has default capacity 10.
Example 1 :-
import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(23); list.add(54.44); list.add("String"); list.add('C'); list.add(23); list.add("xyz"); System.out.println("List is "+list); list.remove("String"); System.out.println("After Remove : "+list); System.out.println("is list contain xyz : "+list.contains("xyz")); System.out.println("NO. of values in list : "+list.size()); System.out.println("is list empty : "+list.isEmpty()); list.clear(); System.out.println("is list empty after clear : "+list.isEmpty()); ArrayList list1 = new ArrayList(); list1.add(34); list1.add("abcd"); list1.add(34.44); list1.add(55); list.addAll(list1); System.out.println("After addAll : "+list); ArrayList list2 = new ArrayList(); list2.add(34); list2.add(55); list2.add("Xyz"); System.out.println("Conatains All : "+list.containsAll(list2)); list.removeAll(list2); System.out.println("After remove all : "+ list); } }
Example 2 :-
import java.util.ArrayList; public class ArrayListMethods { public static void main(String[] args) { ArrayList studId = new ArrayList(); studId.add(45); studId.add(34); studId.add(56); studId.add(12); studId.add(65); studId.add(33); System.out.println(studId); System.out.println("At 2nd index : "+studId.get(2));; studId.add(2, 10); System.out.println(studId); studId.remove(4); System.out.println(studId); studId.set(3, 60); System.out.println(studId); } }
LinkedList
- LinkedList implements the property from List, Dqeue and Queue interface.
- Can Store values dynamically in linked list.
- Can Store different type of values inside linked list.
- LinkedList is based on Doubly linked list Data Structure.
- LinkedList is not based on index, but you will get the methods for index based operations.
- LinkedList is always use for faster modification (addition, removal).
- LinkedList provide slower performance for Iteration/retrieval.
- Random access is not possible in LinkedList.
- LinkedList maintain the insertion order.
- Can perform operation from both the ends in LinkedList using peek, add, remove, pop, poll methods.
Example:-
import java.util.LinkedList; public class LinkedListdemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add(12); list.add("abc"); list.add("xyz"); list.add(12); System.out.println(list); list.addFirst(7); list.addLast("end"); System.out.println("List After add first & last"+list); System.out.println(list.contains("abc")); Object val = list.peek(); System.out.println("peek "+val); System.out.println("After peek "+list); val = list.poll(); System.out.println("poll "+val); System.out.println("After poll "+list); val = list.pop(); System.out.println("pop "+val); System.out.println("After pop "+list); list.push(99); list.push(00); System.out.println(list); } }
Vector
- Vector implements the properties from List interface.
- Can Store values dynamically in Vector.
- Can Store different type of values inside Vector.
- Vector is also based on array data structure.
- Vector allows random access
- Vector is use for faster iteration and slower updation.
- Vector objects are synchronized.
- It is slower than ArrayList.
- Vector objects are thread safe.
- Vector is a legacy class (the class which is introduce in java 1st version)
Join Telegram : Click Here
All Full Stack Java Study Material
Job’s For Fresher