Lists represent an ordered collection of elements. It can have duplicate values. The classes ArrayList, LinkedList, Vector, and Stack implements List Interface.
Syntax –
a) List list1= new ArrayList();
b) List list2 = new LinkedList();
c) List list3 = new Vector();
d) List list4 = new Stack();
Types of List –
- ArrayList –
- The ArrayList class implements the List interface.
- Basically, it uses a dynamic array to store the elements.
- In addition, it maintains the insertion order and is non-synchronized.
- One can randomly access the elements stored in the ArrayList class.
- Eg –
- ArrayList list=new ArrayList();//Creating arraylist
- list.add(“Ram”);//Adding object in arraylist
- LinkedList –
- The LinkedList class implements the List interface.
- Basically, it uses a doubly linked list internally to store the elements.
- In addition, it maintains the insertion order and is not synchronized.
- In the LinkedList, the manipulation is fast because no shifting is required.
- Eg –
- LinkedList list=new LinkedList();
- list.add(“Ram”);
- Vector –
- Vector basically uses a dynamic array to store the data elements.
- It is similar to ArrayList.
- It is synchronized.
- It contains many methods that are not part of the Collection framework.
- Eg –
- Vector v=new Vector();
- v.add(“Ram”);
- Stack –
- A stack is the subclass of Vector and contains all of the methods of Vector class.
- In addition, it implements the last-in-first-out LIFO data structure.
- Eg –
- Stack stack = new Stack();
- stack.push(“Ram”);
- stack.pop();