The LinkedList in Java is a class present in java.util package. LinkedList class is a part of the Java Collection Framework. The LinkedList extends AbstractSequentialList and implements the List interface and Deque interface. LinkedList class in Java provides a linked-list data structure. Java LinkedList class uses the concept of the doubly linked list to store the elements. In the doubly linked list, we can add or remove the elements from both sides.
Java LinkedList is the best choice if we want to perform operations such as insertion or deletion in the middle of the list. It also has a disadvantage if we want to access a node, the nodes cannot be accessed directly instead we need to start from the head and reach to the node we want to access.
1. LinkedList(): The first constructors in used to builds an empty linked list.
2. LinkedList(Collection c): The second constructor is used to builds a linked list that is initialized with the elements of the collection c.
LinkedList<String> list = new LinkedList<String>();
1. boolean add(E e): This method is used to add specified elements at the end of the list.
2. void add(int index, E element): This method is used to add the specified element at a given index.
3. boolean addAll(Collection c): This method is used to add all the elements of the specified collection c to the end of the list
4. boolean addAll(int index, Collection c): This method is used to add all the elements of the specified collection c to the given index of the list.
5. void addFirst(E e): This method is used to add the element to the beginning of the list
6. void addLast(E e): This method is used to add the element to the end of the list
7. void clear(): This method is used to remove all the elements from a list.
8. boolean contains(E e): This method is used to find out the specified element from a list, it returns true if the list contains specified element otherwise it returns false.
9. E get(int index): This method is used to returns the element at the specified position from the list.
10. E getFirst(): This method is used to returns the first element from the list.
11. E getLast(): This method is used to returns the last element from the list
12. int indexOf(Element e): This method is used to returns the index of the specified element. It returns -1 if the list does not contains specified element.
13. int lastIndexOf(Element e): This method is used to returns the index of the last occurrence of the specified element from the list. It returns -1 if the list does not contains specified element.
14. E remove(): This method is used to remove the first element from a list.
15. E remove(int index): This method is used to remove the element from the list which is present at a given index.
16. E removeFirst(): This method is used to remove the first element from the list.
17. E removeLast(): This method is used to remove the last element from the list.
18. boolean removeFirstOccurrence(Object item): This method is used to remove the first occurrence of the specified item when traversing the elements from head to tail.
19. boolean removeLastOccurrence(Object item): This method is used to remove the last occurrence of the specified item when traversing the elements from head to tail.
20. boolean remove(Object o): This method is used to removes the first occurrence of the specified element from the list
add(int index, Element e), addFisrt(Element e), and addLast(Element e). If you want to add an element at the end of the list then you can use the addLast() method. If you want to add an element at the specified position in the list then you can use add(int index, Element e).
Let's understand this with a simple example:
Output:
Let's understand this with a simple example:
Output:
Let's understand this with an example:
Output:
The first element of the list is: Amit
The last element of the list is: Tom
Output:
The element from the specified index is: Raam
2. ArrayList is slow because it internally uses the concept of arrays. If we want to remove any element from an array, all the bits are shifted in memory.
LinkedList is faster than ArrayList because it uses the concept of a doubly-linked list, so bit shifting is not required in memory.
3. ArrayList is best if we want to store and access the data while LinkedList is best if we want to manipulate the data.
4. ArrayList acts as a list because it implements only list interface while LinkedList acts as a list as well as queue because it implements list and Dequeue both interface.
You may also like:
What is Serialization in Java with Example
ArrayList in Java with Example
Exception Handling in Java with Example
Exception Handling in JSP with Example
Different types of JDBC Drivers in Java
Inheritance in Java OOPs with Example
Session Management and its techniques in Servlet
Java 2 Platform Enterprise Edition(J2EE)
Java LinkedList is the best choice if we want to perform operations such as insertion or deletion in the middle of the list. It also has a disadvantage if we want to access a node, the nodes cannot be accessed directly instead we need to start from the head and reach to the node we want to access.
Constructors in Java LinkedList class:
Java LinkedList class has two constructors which are shown below:1. LinkedList(): The first constructors in used to builds an empty linked list.
2. LinkedList(Collection c): The second constructor is used to builds a linked list that is initialized with the elements of the collection c.
How to Create a LinkedList?
We can create a LinkedList by writing a simple statement which is shown below:
LinkedList<String> list = new LinkedList<String>();
This statement creates a linked list with the name list which is of type "String". The type determines which type of elements the Linked List should contain. Since it is of type String, the elements that are going to be added in this list will be of String type.
Methods of Java LinkedList:
1. boolean add(E e): This method is used to add specified elements at the end of the list.
2. void add(int index, E element): This method is used to add the specified element at a given index.
3. boolean addAll(Collection c): This method is used to add all the elements of the specified collection c to the end of the list
4. boolean addAll(int index, Collection c): This method is used to add all the elements of the specified collection c to the given index of the list.
5. void addFirst(E e): This method is used to add the element to the beginning of the list
7. void clear(): This method is used to remove all the elements from a list.
8. boolean contains(E e): This method is used to find out the specified element from a list, it returns true if the list contains specified element otherwise it returns false.
9. E get(int index): This method is used to returns the element at the specified position from the list.
10. E getFirst(): This method is used to returns the first element from the list.
11. E getLast(): This method is used to returns the last element from the list
12. int indexOf(Element e): This method is used to returns the index of the specified element. It returns -1 if the list does not contains specified element.
13. int lastIndexOf(Element e): This method is used to returns the index of the last occurrence of the specified element from the list. It returns -1 if the list does not contains specified element.
14. E remove(): This method is used to remove the first element from a list.
15. E remove(int index): This method is used to remove the element from the list which is present at a given index.
16. E removeFirst(): This method is used to remove the first element from the list.
17. E removeLast(): This method is used to remove the last element from the list.
18. boolean removeFirstOccurrence(Object item): This method is used to remove the first occurrence of the specified item when traversing the elements from head to tail.
19. boolean removeLastOccurrence(Object item): This method is used to remove the last occurrence of the specified item when traversing the elements from head to tail.
20. boolean remove(Object o): This method is used to removes the first occurrence of the specified element from the list
21. int size(): This method returns the number of elements which is present in the list.
22. E set(int index, Element e): This method is used to replace the item of the specified index with the given value.
Java LinkedList Example to add elements:
We can add elements to a linked list by using the following methods add(),add(int index, Element e), addFisrt(Element e), and addLast(Element e). If you want to add an element at the end of the list then you can use the addLast() method. If you want to add an element at the specified position in the list then you can use add(int index, Element e).
Let's understand this with a simple example:
import java.util.
class Example{
public static void main(String args[]){
//creating a linked list
LinkedList<String> list = new LinkedList<String>();
//adding elements to the list
list.add("Amit");
list.add("Rahul");
list.add("Raam");
list.add("Shyam");
list.add("Tom");
list.add("Karan");
//displaying elements
System.out.println("After adding the above elements the list will be:n" +list);
// add element at 3 position.
list.add(2, "Prashant");
//displaying elements after adding at a given index.
System.out.println("After adding the element at a specified index:n" +list);
//adding an element at first position of list
list.addFirst("Vijay");
System.out.println("After adding an element at first position:n" +list);
//adding an element at last position of list
list.addLast("Ramesh");
System.out.println("After adding an element at last position:n" +list);
}
}
Output:
Java LinkedList Example to remove elements:
We can remove elements from a linked list by using the following methods remove(), remove(int index), removeFirst(Element e), and removeLast(Element e). If you want to remove an element at the end of the list then you can use the removeLast() method. If you want to remove an element at the specified position in the list then you can use remove(int index).Let's understand this with a simple example:
import java.util.*;
class Example{
public static void main(String args[]){
//creating a linked list
LinkedList<String> list = new LinkedList<String>();
//adding elements to the list
list.add("Vijay");
list.add("Amit");
list.add("Shyam");
list.add("Tom");
list.add("Amit");
list.add("Tom");
list.add("Karan");
//displaying elements
System.out.pritnln("After adding the above elements the list will be:n" +list);
// removing an element from the 3 position.
list.remove(2);
//displaying elements after removing the 3rd element
System.out.println("After removing the 3rd element:n" + list);
//removing first element
list.removeFirst();
System.out.println("After removing first element:n" +list);
//removing last element
list.removeLast();
System.out.println("After removing last element:n" +list);
//removing first occurrence element
list.removeFirstOccurrence("Amit");
System.out.println("After removing first occurrence element:n" +list);
//removing last occurrence element
list.removeLastOccurrence("Tom");
System.out.println("After removing last occurrence element:n" +list);
}
}
Output:
Java LinkedList example to get the first and last element:
We can use the getFirst() method to get the first element from a list and getLast() method to get the last element from the list.Let's understand this with an example:
import java.util.*;
class Example{
public static void main(String args[]){
//creating a linked list
LinkedList<String> list = new LinkedList<String>();
//adding elements to the list
list.add("Amit");
list.add("Rahul");
list.add("Raam");
list.add("Shyam");
list.add("Tom");
//retrurns first element from a list
Object firstElement = list.getFirst()
System.out.println("The first element of the list is:" +firstElement);
//retrurns last element from a list
Object lastElement = list.getLast()
System.out.println("The last element of the list is:" +lastElement);
}
}
Output:
The first element of the list is: Amit
The last element of the list is: Tom
Java LinkedList example to get the element from the specified index:
We can use get(int index) method to get the element from the specified index of a linked list. Let's understand this with an example:
import java.util.*;
class Example{
public static void main(String args[]){
//creating a linked list
LinkedList<String> list = new LinkedList<String>();
//adding elements to the list
list.add("Amit");
list.add("Rahul");
list.add("Raam");
list.add("Shyam");
list.add("Tom");
//retreive 3rd element
Object element = list.get(2);
System.out.println("The element from the specified index is:" +element);
}
}
Output:
The element from the specified index is: Raam
ArrayList vs LinkedList in Java:
1. ArrayList is implemented with the concept of dynamic arrays while LinkedList is implemented with the concept of a doubly linked list.2. ArrayList is slow because it internally uses the concept of arrays. If we want to remove any element from an array, all the bits are shifted in memory.
LinkedList is faster than ArrayList because it uses the concept of a doubly-linked list, so bit shifting is not required in memory.
3. ArrayList is best if we want to store and access the data while LinkedList is best if we want to manipulate the data.
4. ArrayList acts as a list because it implements only list interface while LinkedList acts as a list as well as queue because it implements list and Dequeue both interface.
You may also like:
What is Serialization in Java with Example
ArrayList in Java with Example
Exception Handling in Java with Example
Exception Handling in JSP with Example
Different types of JDBC Drivers in Java
Inheritance in Java OOPs with Example
Session Management and its techniques in Servlet
Java 2 Platform Enterprise Edition(J2EE)
Java LinkedList Class with Example
Reviewed by Prashant Srivastava
on
February 20, 2019
Rating:
No comments: