Home » Core Java » util » LinkedList » Java LinkedList Example

About Konstantina Dimtsa

Konstantina Dimtsa
Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.

Java LinkedList Example

The LinkedList class can be considered as an alternative to ArrayList class. It extends AbstractSequentialList and implements the List interface, with a doubly-linked list.
Also, the LinkedList allows the usage of iterators, so that you can iterate the list forwards or backwards and declare the exact position of the starting node.

1. Example of LinkedList

Create a java class named LinkedListExample.java with the following code:
 
 
 
 
LinkedListExample.java

package com.javacodegeeks.corejava.linkedlist;

import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListExample {

	public static void main(String args[]) {
		// create a linked list
		LinkedList list = new LinkedList();

		// add elements to the linked list
		list.add("One");
		list.add("Two");
		list.add("Three");
		list.add("Four");
		list.add("Five");

		// create a list iterator
		ListIterator lit = list.listIterator();
		System.out.println("Initial list: ");

		while (lit.hasNext()) {
			System.out.println(lit.next() + " ");
		}

		// add elements in the beginning and in the end of the list
		list.addFirst("Zero");
		list.addLast("Six");

		System.out
				.println("Updated List after insertion in the first and last position: ");
		lit = list.listIterator();
		while (lit.hasNext()) {
			System.out.println(lit.next() + " ");
		}

		System.out.println("Check if list contains the element Four: "
				+ list.contains("Four"));

		System.out.println("The position of the element \"One\" is: "
				+ list.indexOf("One"));

		System.out.println("Get the element in third position of the list: "
				+ list.get(2));

		int size = list.size();
		System.out.println("The size of list is: " + size);

		System.out.println("Iterate List in reverse order: ");
		lit = list.listIterator(size);
		while (lit.hasPrevious()) {
			System.out.println(lit.previous() + " ");
		}
		// remove elements from the linked list
		list.remove("Three");
		list.removeFirst();
		list.removeLast();
		System.out
				.println("Updated List after deletion of the first element, the last element and the element \"Three\": ");
		lit = list.listIterator();
		while (lit.hasNext()) {
			System.out.println(lit.next() + " ");
		}

	}

}

Let’s give a short explanation of the above code. First, we create a linked list of strings and we add items in it. Then, we create a list iterator which will iterate the elements in the list, starting from the beginning. After that, we insert again two elements but in the beginning and in the end of the list, respectively. Then, we use some already known methods from the List class. For example, we use contains method to examine whether a specific element is included in the list, then we use the indexOf method so as to find the position of a specific item, after that we use the get method so as to get an element in the specified position and finally, we use the size method so as to retrieve the size of the list. Then, we use again the iterator of the list but at this time, we define the exact position of the starting node of the iteration. In particular, we start from the end of the list, so as to iterate the list in a reverse order. Then, we delete a specified element in the list and after that, we delete the first and the last item in the list. Finally, we iterate once again the list so as to depict the final version of the list.

If we run the above code, we will have the following results:

  • Output:
Initial list: 
One 
Two 
Three 
Four 
Five 
Updated List after insertion in the first and last position: 
Zero 
One 
Two 
Three 
Four 
Five 
Six 
Check if list contains the element Four: true
The position of the element "One" is: 1
Get the element in third position of the list: Two
The size of list is: 7
Iterate List in reverse order: 
Six 
Five 
Four 
Three 
Two 
One 
Zero 
Updated List after deletion of the first element, the last element and the element "Three": 
One 
Two 
Four 
Five 

2. Download the source code

This was an example of how to use the class LinkedList.
You can download the Eclipse project of this tutorial here: LinkedListExample.zip

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of