Tutorials

Collection Programs


LinkedList traversal


In this chapter our task is:

  • We will create an arraylist
  • We will add few elements in arraylist
  • We will iterate over arraylist using either iterator or enumeration.

We will also see the way to traverse List using List iterator and its difference from iterator at the end of this chapter.

Example: Traverse LinkedList using iterator


package javaRadarLinkedList;

import java.util.Iterator;
import java.util.LinkedList;

public class ItrTraversal {

       public static void main(String[] args) {

       //create Linked List
         LinkedList javaRadarList = new LinkedList();

       //Add elements to LinkedList

        javaRadarList.add("Java");
        javaRadarList.add("Jquery");
        javaRadarList.add("Spring");
        javaRadarList.add("Hibernate");
        javaRadarList.add("EJB");

        //creating iterator to traverse over javaRadarList
         Iterator listIterator = javaRadarList.iterator();

         System.out.println("Traversal in forward direction");

          while(listIterator.hasNext()){

            System.out.println(listIterator.next());

          }

       }

}

OUTPUT:

Traversal in forward direction
Java
Jquery
Spring
Hibernate
EJB
 
 
Example: Traverse LinkedList using Enumeration

	
package javaRadarLinkedList;

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;

public class EnumerationTraversal {

      public static void main(String[] args) {

        //create Linked List
          LinkedList javaRadarList = new LinkedList();

        //Add elements to LinkedList
          javaRadarList.add("Java");
          javaRadarList.add("Jquery");
          javaRadarList.add("Spring");
          javaRadarList.add("Hibernate");
          javaRadarList.add("EJB");

        //creating enumeration to traverse over javaRadarList
          Enumeration listEnumerator = Collections.enumeration(javaRadarList);

          System.out.println("List traversal using Enumeration");

           while(listEnumerator.hasMoreElements()){

              System.out.println(listEnumerator.nextElement());

           }

      }

}

OUTPUT:

List traversal using Enumeration
Java
Jquery
Spring
Hibernate
EJB
 
Example: Traverse LinkedList using ListIterator

	
package javaRadarLinkedList;

import java.util.LinkedList;
import java.util.ListIterator;

public class ListItrTraversal {

      public static void main(String[] args) {

         //create Linked List
           LinkedList javaRadarList = new LinkedList();

         //Add elements to LinkedList
           javaRadarList.add("Java");
           javaRadarList.add("Jquery");
           javaRadarList.add("Spring");
           javaRadarList.add("Hibernate");
           javaRadarList.add("EJB");

         //creating list iterator to traverse over javaRadarList

           ListIterator listIterator = javaRadarList.listIterator();
                               
           System.out.println("Traversal in forward direction");

            while(listIterator.hasNext()){

                 System.out.println(listIterator.next());

            }

                               
             System.out.println("Traversal in backward direction");

              while(listIterator.hasPrevious()){

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

             }

        }

}

OUTPUT:

Traversal in forward direction
Java
Jquery
Spring
Hibernate
EJB
Traversal in backward direction
EJB
Hibernate
Spring
Jquery
Java

 



Please Share this page
Views : 34