LinkedList

LinkedList ListIterator example

This is an example of how to obtain a LinkedList ListIterator. Using a LinkedList ListIterator implies that you should:

  • Create a LinkedList.
  • Populate the list with elements, with add(E e) API method.
  • Obtain a ListIterator, using listIterator() method.
  • For forward iteration over the collection elements invoke hasNext() and next() API methods of ListIterator.
  • For backward iteration over the collection elements invoke hasPrevious() and previous() methods of ListIterator.
  • Invoke nextIndex() and previousIndex() to get next and previous index from the current position in the list.
  • Invoke set(Object o) method to replace the last element returned by next of previous methods.The set method can only be called if neither add or remove methods are called after last call of next or previous methods.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.LinkedList;
import java.util.ListIterator;
 
public class LinkedListListIterator {
 
  public static void main(String[] args) {
 
    // Create a LinkedList and populate it with elements
    LinkedList linkedList = new LinkedList();
    linkedList.add("element_1");
    linkedList.add("element_2");
    linkedList.add("element_3");
    linkedList.add("element_4");
    linkedList.add("element_5");
 
    // The ListIterator object is obtained using listIterator() method
    ListIterator it = linkedList.listIterator();
  
    // For forward iteration over the collection elements we can use hasNext() and next() methods of ListIterator
    System.out.println("Forward iteration :");
    while(it.hasNext())

System.out.println(it.next());
  
    // For backward iteration over the collection elements we can use hasPrevious() and previous() methods of ListIterator
    System.out.println("Backward iteration :");
    while(it.hasPrevious())

System.out.println(it.previous());

    // nextIndex and previousIndex return next and previous index from the current position in the list
    System.out.println("Previous Index is : " + it.previousIndex());   
    System.out.println("Next Index is : " + it.nextIndex());

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());
    
    // nextIndex and previousIndex return next and previous index from the current position in the list
    System.out.println("Previous Index is : " + it.previousIndex());   
    System.out.println("Next Index is : " + it.nextIndex());

    // Add an element just before the next element
    it.add("element_6");

    System.out.println("After inserting element_6, LinkedList contains : ");
    for(int i = 0; i < linkedList.size(); i++)

System.out.println(linkedList.get(i));

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());

    // void remove() method removes the last element returned by next or previous methods
    it.remove();

    System.out.println("After removing element_2, LinkedList contains : ");
    for(int i = 0; i < linkedList.size(); i++)

System.out.println(linkedList.get(i));

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());
    
    /*

void set(Object o) method replaces the last element returned 

by next or previous methods. The set method can only be called 

if neither add or remove methods are called after last call of 

next or previous methods
    */
    it.set("element_7");

    System.out.println("After replacing element_3, LinkedList contains : ");
    for(int i = 0; i < linkedList.size(); i++)

System.out.println(linkedList.get(i)); 
  }
}

Output:

Forward iteration :
element_1
element_2
element_3
element_4
element_5
Backward iteration :
element_5
element_4
element_3
element_2
element_1
Previous Index is : -1
Next Index is : 0
Next element is : element_1
Previous Index is : 0
Next Index is : 1
After inserting element_6, LinkedList contains : 
element_1
element_6
element_2
element_3
element_4
element_5
Next element is : element_2
After removing element_2, LinkedList contains : 
element_1
element_6
element_3
element_4
element_5
Next element is : element_3
After replacing element_3, LinkedList contains : 
element_1
element_6
element_7
element_4
element_5

 
This was an example of how to obtain a LinkedList ListIterator in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button