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.
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment