List in java is the sub-interface of the collection interface present since JDK 1.2. It is used to store multiple elements or Objects as a single entity.
Important points about List Interface
- It is an index-based.
- List can store duplicate data.
- It maintains the insertion order.
- It can store null values or null elements.
Methods in List Interface
method | description | Return Type |
---|---|---|
add(Object o) | It is used to add a new element in the collection. | boolean |
addAll(Collection c) | Used to add a group of elements or another collection’s element an existing Collection. | boolean |
clear() | It is used to remove the elements from the collection. | void |
contains(Object o) | To check particular elements are present in an existing Collection. | boolean |
containsAll(Collection c) | To check a group of elements (Collection c) from the different collections are present or not. | boolean |
isEmpty() | It is used to verify whether the collection has some element or not. | boolean |
iterator() | In order to get a single element at a time from the collection. | Iterator |
remove(Object o) | It is used to remove the particular Object from the collection. | boolean |
removeAll(Collection c) | In order to remove the group of elements (Collection c) from Collection. | boolean |
size() | It is used to count the number of elements or Objects are present in Collection. | int |
toArray() | Used to convert the collection into Array. | Object[] |
retainAll(Collection c) | It is used to remove all objects except the Collection c Object. | boolean |
equals(Object o) | It is used to compare the particular Object with the element present in List. | boolean |
hashCode() | It is used to return the hashCode value of the list. | int |
get(int index) | It fetches the element from a specific position from List. | Object |
lastIndexOf(Object o) | It gives the index value of the last occurrence of the Object in List. It will give -1 if mentioned Object is not in List. | int |
indexOf(Object c) | It is used to fetch the index value of a particular Object. | int |
replaceAll() | It replaced the list of elements with specific elements. | void |
set(int index, E element) | It replaced the element with a new element from the specified position. | Element |
sort(Comparator<? super E> c) | For sorting the elements in List, we use this method. | void |
List has three implementation Classes.
- ArrayList
- LinkedList
- Vector
ArrayList
ArrayList is one of the implementation class of List. It is present since JDK 1.2.
It inherits all the characteristic of List, such as
- ArrayList is the implementation class of List interface.
- The underlying data structure is a resizable or growable array.
- Arraylist allows duplicate values.
- It preserves insertion order.
- It allows NULL insertion
Important points about ArrayList
- ArrayList internally stored the data in the form of Array, with the initial capacity of 10.
- Incremental Capacity of ArrayList is (1.5*(Current Capacity) +1 ).
- Internal Data Structure used is Growable or Resizable Array.
Note: Every collection class implements 2 interfaces – Serializable and Cloneable.
What is Serializable?
The ability to transfer objects across the network is called Serializable.
What is Cloneable?
The ability to create a clone of an object is called Cloneable.
ArrayList has 3 overloaded constructors such as:
- public ArrayList()
- public ArrayList(int Capacity)
- public ArrayList(Collection anotherCollection)
When do you think ArrayList is the best choice?
ArrayList is a good choice for Data retrieval and search operation.
Explanation
ArrayList implements the RandomAccess interface, which provides functionality to access data from any position at the same time. In other words, time taken to access any data from ArrayList is always be same.
Hence, it is a good choice when our requirement is to fetch or retrieve the data from a list of elements.
When do you think ArrayList is the worst choice?
Whenever we try to insert or remove the data in the middle position or at the position other than last, then all the remaining existing elements are going to be shifted. So, performance will become slow because of the shift operation.
Hence, ArrayList is not a better choice the purpose is to insert or remove the data from a list of elements.
What is RandomAccess?
RandomAccess is an interface in java present under java.util package. It is one of the Marker interfaces in java, which is implemented by ArrayList and Vector class. → It provides flexibility to access any element in the ArrayList object at the same speed.
LinkedList
LinkedList is also one of the implementation class of List interface, present since JDK 1.2. It internally stores the data in the form of the node, where each node is connected to the next and previous node, that is each node has the next node and previous node address.
- The underlying data structure is a doubly Linked list.
- It allows duplicate values.
- It preserves insertion order.
- It allows for NULL insertion.
- It implements Serializable and Cloneable interface but not RandomAccess interface.
- The initial capacity concept is not applicable to LinkedList.
LinkedList Constructors :
- LinkedList (): It creates an empty LinkedList object.
- LinkedList (Collection c): it creates an equivalent LinkedList object.
When do you think LinkedList is the best choice?
It is the best choice when our frequent operation is insertion or deletion.
Explanation: Since in LinkedList store the data in the node form and nodes are not connected to each other, they simply storing the address of other nodes. Hence because of insertion and deletion, there will be no shift operations.
Hence, It is the best choice where our requirement is to store a collection of numbers inform LinkedList.
When do you think LinkedList is the worst choice?
If the frequent operation is retrieval, then LinkedList is the worst choice because communication between multiple nodes is very much time-consuming.
Few methods of LinkedList :
- addFirst()
- addLast()
- getFirst()
- getLast()
- removeFirst()
- removeLast()
What are the difference between ArrayList and LinkedList ?
Array | Collection |
---|---|
The underlying data structure is a resizable or growable array. | The underlying data structure is a doubly-linked list. |
ArrayList implements RandomAccess. | LinkedList does not implement RandomAccess. |
ArrayList is the best option when the frequent option is retrieval. | LinkedList is the best option when the frequent operation is insertion or deletion of elements in between. |
An array can store both primitive as well as non-primitive data. | It can only store the non-primitive data. Such as String and Objects. |
ArrayList is the worst choice when the frequent operation is insertion or deletion of elements in between. | LinkedList is the worst choice when frequent operation is retrieval. |
Vector in Java
Vector is one of the implementation class of List Interface present since the JDK 1.0. Since it is present from version 1.0, So, it is also called the Legacy Class.
Important points About Vector
- It is single-threaded or we can say Vector is Thread Safe, that is it safe from the multiple threads.
- Methods present in the Vector are synchronized.
- The Internal Data structure used in Vector is Growable and Resizable Array.
- Vector also internally stores the Data in the form of Array.
- The incremental capacity of Vector is double the current capacities.
Difference between ArrayList and Vector
ArrayList | Vector |
---|---|
ArrayList is multi-threaded. | Vector is Single Threaded. (It is thread-safe.) |
Methods are not synchronized. Hence multiple threads can apply at a time , hence it is not threaded safe. | Methods are synchronized. |
ArrayList is present since jdk 1.2 | Vector is present since jdk 1.0. So, it is a legacy class. |
ArrayList has 3 overloaded constructors. | Vector has 4 overloaded constructors. |
It has increamental capacity of (1.5*currentCapacity + 1). | It has an incremental capacity of 2*currentCapacity. |
Performance-wise ArrayList is faster compare to Vector. Reason: Because of the multi-threaded. | It is not recommended from the Performance aspect. |
Methods in Vector
- addElement(Object o)
- removeElement(Object o)
- removeElementAt(int index)
- removeAllElements()
- elementAt(int index)
- firstElement()
- lastElement()
- size()
- capacity()
- elements()
Thank you so much for reading my article, I hope you got the chance to learn something new from here. If you have any queries or doubts, please feel free to ask any time, You can comment me in down comment box, I assure you, will solve your problem as soon as post your queries. If you want to learn some other topics, please check out my other articles too.