LinkedList

  • The LinkedList is very similar in syntax to ArrayList.
  • We can change ArrayList to a LinkedList by changing the object type.
  • import java.util.LinkedList;
    public class Myclass{
            public static void main(String args[]){
                  LinkedList<String> c = new LinkedList<String>();
                  c.add("Red");
                  c.add("Blue");
                  c.add("Green");
                  c.remove("Green");
                  System.out.println(c);
       }
    }
    Output-
    [Red , Blue]

Note- we cannot specify an initial capacity for LinkedList .

LinkedList Vs ArrayList –

  • The most notable difference between LinkedList and the ArrayList is in the way they store objects.
  • ArrayList is better for storing & accessing data , as it is very similar to a normal array.
  • The LinkedList is better for manipulating data , such as making┬ánumerous insert and deletes.
  • In addition to storing the object , the LinkedList stores the memory address (of link) of the element that it follows.
  • It is called LinkedList because each element contains a link to the neighbouring elements.
  • Note-
  • ArrayList is used when we need rapid access to our data.
  • LinkedList is used when we need to make a large number of insert & deletes.

ArrayList