LinkedList with Example


Linked list implementation of the List interface. Implements all optional list operations, and permits all elements including null. linked list 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.

List list = Collections.synchronizedList(LinkedListobject);

LinkedlIst Example:


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

public class LinkedListExample {
 public static void main(String[] args) {
  LinkedList list = new LinkedList();
  System.out.println("LinkedList Initial Size: " 
    + list.size());
  list.add("a");
  list.add("b");
  list.add(new Integer(5));

  System.out.println("Current LinkedList Size: " 
    + list.size());

  list.addFirst(8);
  System.out.println("The contents of LinkedList: " + list);

  list.addLast("c");
  list.add(2, "d");
  list.add(1, "e");
  list.remove(4);

  System.out.println("The contents of LinkedList: " + list);
  
  ListIterator it = list.listIterator();
  System.out.println("LoadLisnkedList contents using 
                ListIterator:");  
  while(it.hasNext()){
   System.out.print(it.next()+" ");
  }
 }
}
Output:
LinkedList Initial Size: 0
Current LinkedList Size: 3
The contents of LinkedList: [8, a, b, 5]
The contents of LinkedList: [8, e, a, d, 5, c]
Get LisnkedList contents using ListIterator:
8 e a d 5 c 

Search: