LinkedList Example in Java Collection Framework. This Java LinkedList example describes the basic operations performed on the LinkedList in Collection Framework.
Program:
import java.util.*;
class LinkedListDemo{
public static void main(String args[]){
LinkedList l1=new LinkedList();
System.out.println(“Original Contents : ” + l1);
l1.add(“C”);
l1.add(“E”);
l1.add(“B”);
l1.add(“D”);
l1.add(“F”);
l1.addLast(“Z”);
l1.addFirst(“A”);
l1.add(1,”A2″);
System.out.println(“Original Contents after add : ” + l1);
l1.remove(“F”);
l1.remove(2);
System.out.println(“Original Contents after removal : ” + l1);
l1.removeFirst();
l1.removeLast();
System.out.println(“Original Contents : ” + l1);
Object val=l1.get(2);
l1.set(2,(String) val + “Changed”);
System.out.println(“l1 after change : ” + l1);
}
}
Result:
Original Contents : []
Original Contents after add : [A, A2, C, E, B, D, F, Z]
Original Contents after removal : [A, A2, E, B, D, Z]
Original Contents : [A2, E, B, D]
l1 after change : [A2, E, BChanged, D]