Java LinkedList

INTRODUCTION TO JAVA LINKEDLIST

Java LinkedList is used to store an “ordered” group of elements where duplicates are allowed.

A LinkedList is based on a double linked list where elements of the List are typically accessed through add() and remove() methods.

JAVA LINKEDLIST EXAMPLE

Below is a Java LinkedList Example showing how collections are manipulated using an LinkedList

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Random;

public class LinkedListExample {

	public static void main(String[] args) {
		//        LinkedList Creation
		List linkedListA = new LinkedList();
		List linkedListB = new LinkedList();
		//        Adding elements to the LinkedList
		for (int i = 0; i < 5; i++) {
			linkedListA.add(new Integer(i));
		}
		linkedListB.add("beginner");
		linkedListB.add("java");
		linkedListB.add("tutorial");
		linkedListB.add(".");
		linkedListB.add("com");
		linkedListB.add("java");
		linkedListB.add("site");
		//      Iterating through the LinkedList to display the Contents.
		Iterator i1 = linkedListA.iterator();
		System.out.print("LinkedList linkedListA --> ");
		while (i1.hasNext()) {
			System.out.print(i1.next() + " , ");
		}
		System.out.println();
		System.out.print("LinkedList linkedListA --> ");
		for (int j = 0; j < linkedListA.size(); j++) {
			System.out.print(linkedListA.get(j) + " , ");
		}
		System.out.println();
		Iterator i2 = linkedListB.iterator();
		System.out.println("LinkedList linkedListB --> ");
		while (i2.hasNext()) {
			System.out.print(i2.next() + " , ");
		}
		System.out.println();
		System.out.println();
		System.out
		.println("Using ListIterator to retrieve LinkedList Elements");
		System.out.println();
		ListIterator li1 = linkedListA.listIterator();
		//next(), hasPrevious(), hasNext(), hasNext() nextIndex() can be used 
		//  with a ListIterator interface implementation
		System.out.println("LinkedList linkedListA --> ");
		while (li1.hasNext()) {
			System.out.print(li1.next() + " , ");
		}
		System.out.println();
		//        Searching for an element in the LinkedList
		int index = linkedListB.indexOf("java");
		System.out.println("'java' was found at : " + index);
		int lastIndex = linkedListB.lastIndexOf("java");
		System.out.println("'java' was found at : " + lastIndex
				+ " from the last");
		System.out.println();
		//        Getting the subList from the original List
		List subList = linkedListA.subList(3, linkedListA.size());
		System.out.println("New Sub-List(linkedListA) from index 3 to "
				+ linkedListA.size() + ": " + subList);
		System.out.println();
		//        Sort an LinkedList
		System.out.print("Sorted LinkedList linkedListA --> ");
		Collections.sort(linkedListA);
		System.out.print(linkedListA);
		System.out.println();
		//      Reversing an LinkedList
		System.out.print("Reversed LinkedList linkedListA --> ");
		Collections.reverse(linkedListA);
		System.out.println(linkedListA);
		System.out.println();
		//		Checking emptyness of an LinkedList
		System.out.println("Is linkedListA empty?   "
				+ linkedListA.isEmpty());
		System.out.println();
		//        Checking for Equality of LinkedLists
		LinkedList LinkedListC = new LinkedList(linkedListA);
		System.out.println("linkedListA.equals(LinkedListC)? "
				+ linkedListA.equals(LinkedListC));
		System.out.println();
		//      Shuffling the elements of an LinkedList in Random Order
		Collections.shuffle(linkedListA, new Random());
		System.out
		.print("LinkedList linkedListA after shuffling its elements--> ");
		i1 = linkedListA.iterator();
		while (i1.hasNext()) {
			System.out.print(i1.next() + " , ");
		}
		System.out.println();
		System.out.println();
		//        Converting an LinkedList to an Array
		Object[] array = linkedListA.toArray();
		for (int i = 0; i < array.length; i++) {
			System.out.println("Array Element [" + i + "] = " + array[i]);
		}
		System.out.println();
		//        Clearing LinkedList Elements
		linkedListA.clear();
		System.out.println("linkedListA after clearing  : " + linkedListA);
		System.out.println();
	}
}

Output

LinkedList linkedListA –> 0 , 1 , 2 , 3 , 4 ,
LinkedList linkedListA –> 0 , 1 , 2 , 3 , 4 ,
LinkedList linkedListB –>
beginner , java , tutorial , . , com , java , site ,

Using ListIterator to retrieve LinkedList Elements

LinkedList linkedListA –>
0 , 1 , 2 , 3 , 4 ,
‘java’ was found at : 1
‘java’ was found at : 5 from the last

New Sub-List(linkedListA) from index 3 to 5: [3, 4]

Sorted LinkedList linkedListA –> [0, 1, 2, 3, 4] Reversed LinkedList linkedListA –> [4, 3, 2, 1, 0]

Is linkedListA empty? false

linkedListA.equals(LinkedListC)? true

LinkedList linkedListA after shuffling its elements–> 3 , 2 , 4 , 0 , 1 ,

Array Element [0] = 3
Array Element [1] = 2
Array Element [2] = 4
Array Element [3] = 0
Array Element [4] = 1

linkedListA after clearing : []

Download LinkedListExample.java

Like us on Facebook