Home » Core Java » util » LinkedList » LinkedList ListIterator example

About Ilias Tsagklis

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

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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of