• September 3, 2014
  • Core Java
  • Comments Off on ListIterator interface in java

ListIterator interface in java

ListIterator:

ListIterator interface is used to traverse the elements in both forward and backward directions.

Note: Can traverse elements in both forward and backward directions.

Commonly Used methods of ListIterator Interface:

1. hasNext(): Returns true if iterator has more elements in forward direction otherwise returns false.

Syntax: public boolean hasNext().

2. next(): Returns the element and point to the next element.

Syntax: public object next().

3. hasPrevious(): Returns true if iterator has more elements in back direction otherwise returns false.

Syntax: public boolean hasPrevious().

4. previous(): Returns the element and point to the previous element.

Syntax: public object ne previous().

ListIterator interface example:

ListIteratorTest.java

import java.util.ArrayList;
import java.util.ListIterator;
 
/**
 * This class is used to show the ListIterator functionality.
 * @author javawithease
 */
public class ListIteratorTest {
	public static void main(String args[]){
		//Create ArrayList object.
		ArrayList arrayList = new ArrayList();
 
		//Add objects to the HashSet.
		arrayList.add("Amani");
		arrayList.add("Prabhjot");
		arrayList.add("Nidhi");
		arrayList.add("Vandana");
		arrayList.add("Poonam");
 
		//Get the ListIterator object.
		ListIterator  listIterator=arrayList.listIterator();
 
		//Print the ArrayList elements in forward direction.
        	System.out.println("ArrayList elements in " + 
                                          "forward direction:");
		while(listIterator.hasNext()){  
		   System.out.println(listIterator.next());  
		} 
 
		//Print the ArrayList elements in backward direction.
		System.out.println("ArrayList elements in " +
				"backward direction:");
		while(listIterator.hasPrevious()){  
		   System.out.println(listIterator.previous());  
		} 
	}
}

Output:

ArrayList elements in forward direction:
Amani
Prabhjot
Nidhi
Vandana
Poonam
ArrayList elements in backward direction:
Poonam
Vandana
Nidhi
Prabhjot
Amani

Download this example.

 
Next Topic: Servlet Tutorial with example.
Previous Topic: Hashtable in java with example.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.