LinkedList In Java | Java Tutorial
Earlier we have seen Arrays and ArrayList. In this post, we will see LinkedList in Java. LinkedList is a class in the Collection Framework. LinkedList class implements List and Deque interfaces. LinkedList class extends AbstractList class. Lets see some key points on LinkedList.
- LinkedList class can hold duplicate elements in list.
- LinkedList is used to create an empty linked list.
- LinkedList class maintains insertion order.
- LinkedList class is non synchronized.
- In LinkedList class, manipulation is fast because shifting is not required when new element is inserted or deleted from the list.
Java LinkedList Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
package samplePackage; import java.util.LinkedList; public class LinkedListExample { public static void main(String args[]) { //Creating linked list object LinkedList<String> lref = new LinkedList<String>(); // Adding elements using add method lref.add("IDE"); lref.add("RC"); lref.add("WebDriver"); lref.add("Grid"); System.out.println(lref); // Adding element at first position in list using addFirst lref.addFirst("QTP"); // Adding element at last position in list using addLast lref.addLast("Protractor"); System.out.println(lref); // Add elements at index 1 in list lref.add(0, "Mercury"); System.out.println(lref); // Removing first item from list using removeFirst method lref.removeFirst(); // Removing last item from list using removeLast method. lref.removeLast(); System.out.println(lref); // Removing item from list using value lref.remove("Two"); System.out.println(lref); // Removing item from list using index lref.remove(3); System.out.println(lref); // Get item from list using index System.out.println(lref.get(0)); // Set item at given index in list lref.set(3, "New Entry"); System.out.println(lref); // We have four ways to print all the values of linked list System.out.println("====== For Loop starts here ======"); for(int l=0; l<lref.size(); l++) { System.out.println(lref.get(l)); } System.out.println("====== For Loop ends here ======"); System.out.println("====== For Each starts here ======"); for(String str:lref) { System.out.println(str); } System.out.println("====== For Each ends here ======"); System.out.println("====== Iterator starts here ======"); Iterator <String> it = lref.iterator(); while(it.hasNext()){ System.out.println(it.next()); } System.out.println("====== Iterator ends here ======"); System.out.println("====== While starts here ======"); int num=0; while(lref.size()>num){ System.out.println(lref.get(num)); num=num+1; } System.out.println("====== While ends here ======"); } } |
1 2 3 4 5 6 7 8 |
[IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid, Protractor] [Mercury, QTP, IDE, RC, WebDriver, Grid, Protractor] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, Grid] QTP [QTP, IDE, RC, New Entry] |
LinkedList using For loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package samplePackage; import java.util.LinkedList; public class LinkedListExample { public static void main(String args[]) { //Creating linked list object LinkedList<String> lref = new LinkedList<String>(); // Adding elements using add method lref.add("IDE"); lref.add("RC"); lref.add("WebDriver"); lref.add("Grid"); System.out.println(lref); // Adding element at first position in list using addFirst lref.addFirst("QTP"); // Adding element at last position in list using addLast lref.addLast("Protractor"); System.out.println(lref); // Add elements at index 1 in list lref.add(0, "Mercury"); System.out.println(lref); // Removing first item from list using removeFirst method lref.removeFirst(); // Removing last item from list using removeLast method. lref.removeLast(); System.out.println(lref); // Removing item from list using value lref.remove("Two"); System.out.println(lref); // Removing item from list using index lref.remove(3); System.out.println(lref); // Get item from list using index System.out.println(lref.get(0)); // Set item at given index in list lref.set(3, "New Entry"); System.out.println(lref); System.out.println("====== For Loop starts here ======"); for(int l=0; l<lref.size(); l++) { System.out.println(lref.get(l)); } System.out.println("====== For Loop ends here ======"); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid, Protractor] [Mercury, QTP, IDE, RC, WebDriver, Grid, Protractor] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, Grid] QTP [QTP, IDE, RC, New Entry] ====== For Loop starts here ====== QTP IDE RC New Entry ====== For Loop ends here ====== |
LinkedList with Enhanced For Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package samplePackage; import java.util.LinkedList; public class LinkedListExample { public static void main(String args[]) { //Creating linked list object LinkedList<String> lref = new LinkedList<String>(); // Adding elements using add method lref.add("IDE"); lref.add("RC"); lref.add("WebDriver"); lref.add("Grid"); System.out.println(lref); // Adding element at first position in list using addFirst lref.addFirst("QTP"); // Adding element at last position in list using addLast lref.addLast("Protractor"); System.out.println(lref); // Add elements at index 1 in list lref.add(0, "Mercury"); System.out.println(lref); // Removing first item from list using removeFirst method lref.removeFirst(); // Removing last item from list using removeLast method. lref.removeLast(); System.out.println(lref); // Removing item from list using value lref.remove("Two"); System.out.println(lref); // Removing item from list using index lref.remove(3); System.out.println(lref); // Get item from list using index System.out.println(lref.get(0)); // Set item at given index in list lref.set(3, "New Entry"); System.out.println(lref); System.out.println("====== Enhanced For loop starts here ======"); for(String str:lref) { System.out.println(str); } System.out.println("====== Enhanced For loop ends here ======"); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid, Protractor] [Mercury, QTP, IDE, RC, WebDriver, Grid, Protractor] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, Grid] QTP [QTP, IDE, RC, New Entry] ====== Enhanced For loop starts here ====== QTP IDE RC New Entry ====== Enhanced For loop ends here ====== |
LinkedList using Iterator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package samplePackage; import java.util.Iterator; import java.util.LinkedList; public class LinkedListExample { public static void main(String args[]) { //Creating linked list object LinkedList<String> lref = new LinkedList<String>(); // Adding elements using add method lref.add("IDE"); lref.add("RC"); lref.add("WebDriver"); lref.add("Grid"); System.out.println(lref); // Adding element at first position in list using addFirst lref.addFirst("QTP"); // Adding element at last position in list using addLast lref.addLast("Protractor"); System.out.println(lref); // Add elements at index 1 in list lref.add(0, "Mercury"); System.out.println(lref); // Removing first item from list using removeFirst method lref.removeFirst(); // Removing last item from list using removeLast method. lref.removeLast(); System.out.println(lref); // Removing item from list using value lref.remove("Two"); System.out.println(lref); // Removing item from list using index lref.remove(3); System.out.println(lref); // Get item from list using index System.out.println(lref.get(0)); // Set item at given index in list lref.set(3, "New Entry"); System.out.println(lref); System.out.println("====== Iterator starts here ======"); Iterator <String> it = lref.iterator(); while(it.hasNext()){ System.out.println(it.next()); } System.out.println("====== Iterator ends here ======"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid, Protractor] [Mercury, QTP, IDE, RC, WebDriver, Grid, Protractor] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, WebDriver, Grid] [QTP, IDE, RC, Grid] QTP [QTP, IDE, RC, New Entry] ====== Iterator starts here ====== QTP IDE RC New Entry ====== Iterator ends here ====== |
Methods for Java LinkedList:
- int size(): It returns the number of elements in the list.
- void clear(): It removes all of the elements from the list.
- Object set(int index, Object element): It is used to replace an existing element in the list with a new element.
- void add(int index, Object element): It inserts the element at the position ‘index’ in the list.
- void addFirst(Object element): It inserts the element at the beginning of the list.
- void addLast(Object element): It appends the element at the end of the list.
- Object get(int index): It returns the element at the position ‘index’ in the list. It throws ‘IndexOutOfBoundsException’ if the index is out of range of the list.
- Object getFirst(): It returns the first element of the Linked List.
- Object getLast(): It returns the last element of the Linked List.
- int indexOf(Object element): If element is found, it returns the index of the first occurrence of the element. Else, it returns -1.
- int lastIndexOf(Object element): If element is found, it returns the index of the last occurrence of the element. Else, it returns -1.
- Object remove(): It is used to remove and return the element from the head of the list.
- Object remove(int index): It removes the element at the position ‘index’ in this list. It throws ‘NoSuchElementException’ if the list is empty.
- Object removeLast(): It is used to remove and return the last element of the Linked List.
Must Read: Java Tutorial