Image

Java - Core Java - Collection Framework - LinkedList

LinkedList

  • The underlying Data Structure is doubly linked list.
  • Insertion order is preserved.
  • Duplicate objects are allowed.
  • Heterogeneous objects are allowed.
  • Null insertion is possible.
  • LinkedList implements Serializable and Cloneable interfaces but not the RandomAccess.
  • Linked list is the best choice if our frequent operation is insertion and deletion in middle.
  • Linked list is the worst choice if our frequent operation is retrieval operation.
  • Program
    import java.util.LinkedList;
    public class LinkedListDemo1 {
        public static void main(String[] args) {
            LinkedList ll=new LinkedList();
            ll.add("Ajay");
            ll.add(30);
            ll.add(null);
             System.out.println(ll);
            ll.set(2, "Software");
             System.out.println(ll);
            ll.add(2, "APS");
             System.out.println(ll);
            ll.removeLast();
             System.out.println(ll);
            ll.addFirst("ccc");
            System.out.println(ll);
        }
    }   
    O/P
    [Ajay, 30, null]
    [Ajay, 30, Software]
    [Ajay, 30, APS, Software]
    [Ajay, 30, APS]
    [ccc, Ajay, 30, APS]