Friday, 17 March 2017

LinkedList class


LinkedList class belongs to java.util package.
It implements List, DequeCloneable and Serializable interface. LinkedList is the doubly-LinkedList implementation of the List and Deque interface.


Key feature of LinkedList:

1.      It is non-synchronized.
2.      LinkedList is preferred over ArrayList if operations like insertion or deletion needs to be done in bulk.
3.      LinkedList allows addition of null element.

Difference      

Now let’s look at the ways in which you can create LinkedList in java. Please click on each to learn the creation technique.

·         LinkedList<E> myList = new LinkedList <E>();   //<E> denotes Generics usage
·         LinkedList<E> myList = new LinkedList <E>(Collection c);

Simple Example:

package javaRadarLinkedList;

import java.util.LinkedList;

public class SimpleLinkedList {

      public static void main(String[] args) {
            //create LinkedList
            LinkedList<String> javaRadarList = new LinkedList<String>();
            //Add elements to LinkedList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }


}


Now let’s look at different ways in which you can use the LinkedList that you created.

LinkedList operations

·         Search element in LinkedList

Traverse LinkedList


Convert

·         LinkedList to ArrayList


ArrayList or LinkedList. Which one to use?(Click to see the difference between two and decide)

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.