LinkedList in Java


We will cover following points of LinkedList in Java:

  1. Basics of LinkedList in Java
  2. How to iterate through LinkedList in Java
  3. Constructor and methods of LinkedList in Java

Basics of LinkedList in Java

  • LinkedList is a class which implements List, Deque, Cloneable and Serializable interface.
  • LinkedList uses node representation to store the elements.
  • LinkedList store the elements in added order.
  • It allows duplicate elements.
  • We can have null as an element in LinkedList.
  • LinkedList elements can be accessed by using Iterator and ListIterator interface.
  • LinkedList objects are eligible for Serializable and Cloning.

How to iterate LinkedList in Java

  1. Using iterator
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

public class LinkedListExample {
     public static void main(String[] args) {
            List<String> list = new LinkedList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
     Iterator<String> it = list.iterator();
       while (it.hasNext()) {
         System.out.println(it.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using Enumeration
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

public class LinkedListExample{
     public static void main(String[] args) {
            LinkedList<String> list = new LinkedList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
     Enumeration<String> enm = list.elements();
       while (enm.hasMoreElements()) {
         System.out.println(it.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using ListIterator
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

public class LinkedListExample {
     public static void main(String[] args) {
            List<String> list = new LinkedList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
      ListIterator<String> listIter = list.listIterator();
       while (listIter .hasNext()) {
        System.out.println(listIter.next());
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using For-each loop
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

public class LinkedListExample {
     public static void main(String[] args) {
            LinkedList<String> list = new LinkedList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
       for (String str : list) {
         System.out.println(list.get(i));
       }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using For loop
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

public class LinkedListExample {
     public static void main(String[] args) {
            List<String> list = new LinkedList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
           for(int i = 0; i < list.size();i++) { 
            System.out.println(list.get(i)); 
           }
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash
  1. Using Java 8 for-each expression
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;

public class LinkedListExample {
     public static void main(String[] args) {
            List<String> list = new LinkedList<>();
            list.add("Nadeem");
            list.add("Puneet");
            list.add("Shyam");
            list.add("Jagdish");
            list.add("Aakash");
            list.forEach(str -> {
               System.out.println(str);
            });
     }
}
Nadeem
Puneet
Shyam
Jagdish
Aakash

Constructor and methods of LinkedList in Java

LinkedList in Java has mainly 2 constructor:

Learn more about collection in java


%d bloggers like this: