Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

LinkedList in Collection class Hierarchy:-


Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements. (including null).

All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

Note that this implementation is not synchronized. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list.

List list = Collections.synchronizedList(new LinkedList(...));

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

LinkedList Example with String:-

import java.util.*;
public class LinkedListDemo {
	public static void main(String[] args) {
	// Generic Declaration
	// collection classes can be used as container for other objects.
	//Default capacity of LinkedList is 0
	List<String> list = new LinkedList<String>(); 
	list.add("Apple");
	list.add("Mango");
	list.add("Orange");
	list.add("Banana");
	list.add("Papaya");
	list.add("Papaya");//adding duplicate element.

	Iterator it = list.iterator();
	//Iterator can be used to access any collection which 
	//implements Iterable interface
	while (it.hasNext()) {
		String element = it.next();
		System.out.println(element);
	}

	//retrieving all the elements.jdk 1.5
	for (String str : list) {
		System.out.println(str);
	}

	// retrieving all the elements.
	for (int i = 0; i < list.size(); i++) {
		System.out.println("list.get(i));//A[i]
	}

	ListIterator listIterator = list.listIterator();
	while (listIterator.hasNext()) {
		System.out.println("listIterator.next());
		listIterator.add("Ankit");
	}

	//searching an element Apple inside the LinkedList
	boolean b=list.contains("Apple"); //complexity  = O(n)		
  }
}

LinkedList Example with Biscuit class:-

Defining a class Biscuit

LinkedList code with Biscuit

Searching a Biscuit in ArrayList Searching complexity of an element into LinkedList is O(n).So LinkedList is not good for searching.