Java Collections Framework:LinkedList-4(LinkedList Iterator/ListIterator)

 

Java – LinkedList Iterator example



Example

The steps we followed in the below program are:

1) Create a LinkedList
2) Add element to it using add(Element E) method
3) Obtain the iterator by calling iterator() method
4) Traverse the list using hasNext() and next() method of Iterator class.

import java.util.LinkedList;
import java.util.Iterator;
public class IteratorExample {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Delhi");
    linkedlist.add("Agra");
    linkedlist.add("Mysore");
    linkedlist.add("Chennai");
    linkedlist.add("Pune");
 
    // Obtaining Iterator
    Iterator it = linkedlist.iterator();
 
    // Iterating the list in forward direction
    System.out.println("LinkedList elements:");
    while(it.hasNext()){
       System.out.println(it.next());
    }
 }
}

Output:



LinkedList elements:
Delhi
Agra
Mysore
Chennai
Pune

Java – LinkedList ListIterator example


In this example we will see how to iterate a LinkedList using ListIterator. Using Listterator we can iterate the list in both the directions(forward and backward). Along with traversing, we can also modify the list during iteration, and obtain the iterator’s current position in the list. Read more about it at ListIterator javadoc.

Example

Here we have a LinkedList of Strings and we are traversing it in both the directions using LitIterator.

import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorExample {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Delhi");
    linkedlist.add("Agra");
    linkedlist.add("Mysore");
    linkedlist.add("Chennai");
    linkedlist.add("Pune");
 
    // Obtaining ListIterator
    ListIterator listIt = linkedlist.listIterator();
 
    // Iterating the list in forward direction
    System.out.println("Forward iteration:");
    while(listIt.hasNext()){
       System.out.println(listIt.next());
    }

    // Iterating the list in backward direction
    System.out.println("\nBackward iteration:");
    while(listIt.hasPrevious()){
       System.out.println(listIt.previous());
    } 
 }
}

Output:

Forward iteration:
Delhi
Agra
Mysore
Chennai
Pune

Backward iteration:
Pune
Chennai
Mysore
Agra
Delhi

Iterate a LinkedList in reverse sequential order – java


Description

Program to iterate a LinkedList in reverse order using descendingIterator() method of LinkedList class.

Program

import java.util.LinkedList;
import java.util.Iterator;
class LinkedListDemo {

  public static void main(String[] args) {

     // create a LinkedList
     LinkedList<String> list = new LinkedList<String>();

     // Adding elements to the LinkedList
     list.add("Element1");
     list.add("Element2");
     list.add("Element3");
     list.add("Element4");

     // Displaying LinkedList elements
     System.out.println("LinkedList elements: "+list);

     /* public Iterator<E> descendingIterator(): Returns an 
      * iterator over the elements in this list in reverse 
      * sequential order. The elements will be returned in 
      * order from last (tail) to first (head).
      */
     Iterator it = list.descendingIterator();

     // Displaying list in reverse order
     System.out.println("Elements in Reverse Order:");
     while (it.hasNext()) {
        System.out.println(it.next());
     }
  }
}

Output:

LinkedList elements: [Element1, Element2, Element3, Element4]
Elements in Reverse Order:
Element4
Element3
Element2
Element1

Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Selenium-Java Contents

Java OOPS:Constructors in Java – A complete study!!