In this article, we will discuss LinkedList class – one of the List implemented class in detail
1. LinkedList:
- LinkedList is implementation class of List interface (i.e.; LinkedList implements List)
- LinkedList uses doubly-linked list to store element/objects
- Duplicate element/objects are allowed to be inserted into LinkedList
- Insertion order is maintained
- Allows NULL insertion (no limit, we can insert any number of NULL to LinkedList)
- Without generics, LinkedList allows to insert any type of objects;
- With generics, it is type-bounded (except, if we take Object as type within angle brackets)
- Manipulation (i.e.; addition/deletion/updation of element from middle of LinkedList) is very fast, as it works on the basis of previous & next nodes and there is no shifting required
- Elements retrieval is much slower, as LinkedList need to iterate over complete list to retrieve/get the required item/element
- LinkedList is non-synchronized
- Present in java.util package and extends java.util.AbstractSequentialList implements java.util.List interface
- Also, implements java.lang.Cloneable, java.io.Serializable marker interfaces which provides special ability to LinkedList (provided by JVM at run time) like,
- java.lang.Cloneable: to create a duplicate object or to clone an object
- java.io.Serializable: to transfer objects across network
Source: Team BenchResources.Net
2. LinkedList constructors:
2.1 LinkedList ll = new LinkedList();
- creates an empty LinkedList object
2.2 LinkedList ll = new LinkedList(Collection c);
- creates a LinkedList for the specified collection
- it is basically used for inter-conversion between collection objects
3. LinkedList methods:
- We can use LinkedList as Stack or Queue.
- To work with stack or queue,
- LinkedList defines more specific methods as listed below
LinkedList methods | Description |
void addFirst(Object obj); | add/inserts the specified element/object at the beginning of the invoking list |
void addLast(Object obj); | add/inserts the specified element/object at the end of the invoking list |
Object getFirst(); | returns first element/object from invoking list
throws NoSuchElelementException; if list is empty |
Object getLast(); | returns last element/object from invoking list
throws NoSuchElelementException; if list is empty |
Object removeFirst(); | removes & returns first element/object from invoking list
throws NoSuchElelementException; if list is empty |
Object removeLast(); | removes & returns last element/object from invoking list
throws NoSuchElelementException; if list is empty |
Iterator descendingIterator(); | Returns an iterator over the elements in this deque in reverse sequential order
The elements will be returned in order from last (tail) to first (head) Available from Java 1.6 version |
4. LinkedList examples:
LinkedListAddAndRemove.java
package in.bench.resources.java.collection;
import java.util.LinkedList;
public class LinkedListAddAndRemove {
public static void main(String[] args) {
// creating LinkedList object of type String
LinkedList<String> ll = new LinkedList<String>();
// adding elements to LinkedList object
ll.add("Sundar Pichai");
ll.add("Satya Nadella");
ll.add("Shiv Nadar");
ll.add("Shantanu Narayen");
ll.add("Sundar Pichai");
ll.add("Francisco D’Souza");
System.out.println("Iterating LinkedList values\n");
// Iterating using enhanced for-loop
for(String str : ll){
System.out.println(str);
}
// removing element at 4th index
ll.remove(4);
// to print all values of LinkedList
System.out.println("\n\nLinkedList values after removal"
+ " at 4th index postion \n" + ll);
}
}
Output:
Iterating LinkedList values
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
LinkedList values after removal at 4th index postion
[Sundar Pichai, Satya Nadella, Shiv Nadar,
Shantanu Narayen, Francisco D’Souza]
Note: All methods of LinkedList is non-synchronized
Q) How to make LinkedList synchronized ?
- LinkedList can be easily converted into synchronized LinkedList using utility method of java.util.Collections class
List list = Collections.synchronizedList(ll);
Related Articles:
- List interface
- ArrayList class
- LinkedList class
- Vector class
- Stack class
- ArrayList v/s Vector
- ArrayList v/s LinkedList
- List v/s Set
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
Happy Coding !!
Happy Learning !!