Javaexercise.com

Updated On 2022-07-01

Java LinkedList Introduction

Java LinkedList is an implementation class of List interface in Collection Framework. It implements a doubly-linked(data structure) list internally to store data. It allows to perform all list operations and permits all elements including null value.

It was added in the Java 1.2 version and is located in java.util package of java.base module. In this article, we will see some example to understand the working of LinkedList. The syntax of the class is like:

LinkedList Class Declaration

public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable

 

Properties of ArrayList:

  • Duplicates allowed: It can contain duplicate elements.
  • Ordered: It maintains insertion order.
  • Null allowed: It can contain a null value.
  • Non synchronized: It is not synchronized.
  • Index-based: It allows to access elements using index positions. Index start with '0'.
  • Dynamic resizing: It resizes dynamically when more elements need to be added than its current size.

 

ArrayList Simple Example

Let's start with creating a LinkedList and add elements to it by using the add() method. Here, we are creating a collection of student data and storing it into a LinkedList. 

import java.util.LinkedList;
/* 
 * Code example to create LinkedList in Java
 */
public class JExercise {
	public static void main(String[] args) {

		LinkedList<String> students = new LinkedList<String>();
		students.add("Kunal");
		students.add("Rohan");
		students.add("David");
		System.out.println(students);
	}
}

Output:

[Kunal, Rohan, David]

 

How to Create LinkedList in Java?

To create a LinkedList, the LinkedList class provides two constructors. one is used to create an empty list while the other is used to create LinkedList with elements of a collection such as ArrayList. Let's see the table below.

ArrayList Constructors

Constructor Description
LinkedList() It is used to construct an empty list.
LinkedList​(Collection<? extends E> c) It is used to construct a list containing the elements of the specified collection, in the order, they are returned by the collection's iterator.

 

Creating a LinkedList with List elements

Here, we used the second constructor to create LinkedList from the list elements. It is helpful when we want to create LinkedList from the existing collections such as list, set, etc.

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/* 
 * Code example to create LinkedList from other collection(list) in Java
 */
public class JExercise {
	public static void main(String[] args) {
		List<String> list = Arrays.asList("Kunal","Rohan","David");
		// Get LinkedList from a List  
		LinkedList<String> students = new LinkedList<String>(list);
		System.out.println(students);
	}
}

Output:

[Kunal, Rohan, David]
 

Operations On LinkedList

LinkedList allows all the common operations such as adding, removing and accessing elements. Here, we will see some examples to work with LinkedList.

Example: Adding Elements

LinkedList class provides several methods to add elements such as add() method add the elements in a sequence while the addFirst() method adds the elements at the starting of LInkedLIst and addLast() method adds the elements at the end of LInkedList. We can use these methods according to the need. See the example where we used all these methods.

import java.util.LinkedList;
/* 
 * Code example to create LinkedList and elements in Java
 */
public class JExercise {
	public static void main(String[] args) {
		LinkedList<String> students = new LinkedList<String>();
		students.add("Kunal");
		students.add("Rohan");
		students.add("David");
		System.out.println(students);
		// Adding element at starting of list
		students.addFirst("Mohan");
		System.out.println(students);
		// Adding element at specified index
		students.add(2,"John");
		System.out.println(students);
		// Adding element at end of the list
		students.addLast("Hemant");
		System.out.println(students);
	}
}

Output:

[Kunal, Rohan, David]
[Mohan, Kunal, Rohan, David]
[Mohan, Kunal, John, Rohan, David]
[Mohan, Kunal, John, Rohan, David, Hemant]
 

Example: Removing Elements

LinkedList class provides several methods to remove elements such as remove() method remove the elements in a sequence while the removeFirst() method removes the elements from the starting of LInkedLIst and removeLast() method removes the elements from the end of LInkedList. We can use these methods according to the need. See the example where we used all these methods.

import java.util.LinkedList;
/* 
 * Code example to create LinkedList and elements in Java
 */
public class JExercise {
	public static void main(String[] args) {
		LinkedList<String> students = new LinkedList<String>();
		students.add("Kunal");
		students.add("Rohan");
		students.add("David");
		System.out.println(students);
		// Remove first element
		students.removeFirst();
		System.out.println(students);
		// Remove from specified index
		students.remove(1);
		System.out.println(students);
		// Remove last element
		students.removeLast();
		System.out.println(students);

	}
}

Output:

[Kunal, Rohan, David]
[Rohan, David]
[Rohan]
[]
 

Example: Access Elements and Find the size of the linked list

Like adding and removing methods, LinkedList class provides several accessibility methods as well such as get() method gets the elements in a sequence while the getFirst() method returns the elements of the starting of LInkedLIst and getLast() method returns the elements from the end of LInkedList. We can use these methods according to the need. See the example where we used all these methods.

import java.util.LinkedList;
/* 
 * Code example to create LinkedList and Access elements
 */
public class JExercise {
	public static void main(String[] args) {
		LinkedList<String>students = new LinkedList<String>();
		students.add("Kunal");
		students.add("Rohan");
		students.add("David");
		System.out.println(students);
		System.out.println(students.size());
		// Get first element
		String fName = students.getFirst();
		System.out.println(fName);
		// Get last element
		String lName = students.getLast();
		System.out.println(lName);
		// Get element of specified index
		System.out.println(students.get(1));
	}
}

Output:

[Kunal, Rohan, David]
3
Kunal
David
Rohan
 

Example: Traversing LinkedList

To traverse all the elements of the LinkedList, we can use any loop such as for loop, for-each loop, etc. See, in the below example, we used both to traverse LinkedList.

import java.util.LinkedList;
/* 
 * Code example to create LinkedList and Traverse it
 */
public class JExercise {
	public static void main(String[] args) {
		LinkedList<String> students = new LinkedList<String>();
		students.add("Kunal");
		students.add("Rohan");
		students.add("David");
		// Traversing using for loop
		System.out.println("Traversing using for loop");
		for (int i = 0; i < students.size(); i++) {
			System.out.println(students.get(i));
		}
		// Traversing using for-each loop
		System.out.println("\nTraversing using for-each loop");
		for(String name: students) {
			System.out.println(name);

		}
	}
}

Output:

Traversing using for loop
Kunal
Rohan
David

Traversing using for-each loop
Kunal
Rohan
David
 

 

Example: Traversing using Iterator

Iterator is an interface in Collection framework which gives iteration feature to all the collection implementation classes such as ArrayList, LInkedList, etc. 

import java.util.Iterator;
import java.util.LinkedList;
/* 
 * Code example to create LinkedList
 */
public class JExercise {
	public static void main(String[] args) {
		LinkedList<String> students = new LinkedList<String>();
		students.add("Kunal");
		students.add("Rohan");
		students.add("David");
		// Traversing using iterator
		System.out.println("Traversing using Iterator");
		Iterator<String> itr = students.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}
	}
}

Output:

Traversing using Iterator
Kunal
Rohan
David
 

Example: Modifying LinkedList Elements

The LinkedList class provides one more method set() that is used to modify any element in the LInkedList. This method takes two arguments, one is index and the second is value. So, the value will be replaced at the specified index location. See the example below.

import java.util.LinkedList;
/* 
 * Code example to create LinkedList
 */
public class JExercise {
	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>();
		linkedList.add("Sohan");
		linkedList.add("Mohan");
		linkedList.add("Mac");
		linkedList.add("David");
		System.out.println(linkedList);
		// Modify element
		linkedList.set(2,"Shubham");
		System.out.println(linkedList);
		linkedList.set(3, "Jack");
		System.out.println(linkedList);
		// Get size of linkedlist
		int size = linkedList.size();
		System.out.println(size);
	}
}

Output:

[Sohan, Mohan, Mac, David]
[Sohan, Mohan, Shubham, David]
[Sohan, Mohan, Shubham, Jack]
4