Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

LinkedList

  • • Like ArrayList, LinkedList is another implementation class of List interface.
  • • Underlying data structure is the double linked list.
  • • >Duplicates are allowed.
  • • Insertion order is preserved.
  • • Null insertion is possible.
  • • LinkedList implements Serializable and Clonable interface, but not RandomAccess interface.
  • • If our frequent operation is the insertion and deletion in the middle, then LinkedListis the best choice.
  • • If our frequent operation is the retrieval operation then LinkedList is the worst choice. Because here retrieval of data items is a time consuming process.
  • • Usually we can use LinkedList to implement Stacks and Queues to provide support for this requirement. LinkedList class defines following specific methods:

voidaddFirst();
voidaddLast();
Object getFirst();
Object getLast();
Object removeFirst();
Object removeLast();

Constructors:

  • • Creates an empty LinkedList Object.

    LinkedList l=new LinkedList();

  • • Creates an equivalent LinkedList Object for the given Collection.

    LinkedList l=new LinkedList(Collection c);

Example:
LinkedListDemo.java:
import java.util.*;
class LinkedListDemo
{
	public static void main(String[] args)
	{
		LinkedList l=new LinkedList();
		l.add("Tapuuu");
		l.add("Mona");
		l.add("Divya");
		l.add(25);
		l.add(null);
		System.out.println(l);
		Iterator it=l.iterator();
		while(it.hasNext())
		{
			Object o1=it.next();
			System.out.println(o1);
		}
		l.addFirst("Tilan");
		l.addLast("Kandhei");
		System.out.println(l);
		l.remove(5);
		l.removeFirst();
		l.removeLast();
		System.out.println(l);
	}
}
Output

[Tapuuu,Mona,Divya,25,null]
Tapuuu
Mona
Divya
25
Null
[Tilan,Tapuuu,Mona,Divya,25,null,Kandhei]
[Tapuuu,Mona,Divya,25]


ArrayList vs. LinkedList:

ArrayList:
  • • It is the best choice if our frequent operation is retrieval.
  • • It is the worst choice if our frequent operation is insertion and deletion.
  • • Underlying data structure for ArrayList is resizable or growable array.
  • • It implements RandomAccess interface.
LinkedList:
  • • It is the best choice if our frequent operation is insertion and deletion.
  • • It is the worst choice if our frequent operation is retrieval operation.
  • • Underlying data structure is double linked list.
  • • It does not implement RandomAccess interface.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext