The perfect place for easy learning...

Java Programming

×

Topics List


Accessing a Java Collection via a Iterator





The java collection framework often we want to cycle through the elements. For example, we might want to display each element of a collection. The java provides an interface Iterator that is available inside the java.util package to cycle through each element of a collection.

🔔 The Iterator allows us to move only forward direction.

🔔 The Iterator does not support the replacement and addition of new elements.

We use the following steps to access a collection of elements using the Iterator.

  • Step - 1: Create an object of the Iterator by calling collection.itertor( ) method.
  • Step - 2: Use the method hasNext( ) to access to check does the collection has the next element. (Use a loop).
  • Step - 3: Use the method next( ) to access each element from the collection. (use inside the loop).

The Iterator has the following methods.

Method Description
Iterator iterator( ) Used to obtain an iterator to the start of the collection.
boolean hasNext( ) Returns true if the collection has the next element, otherwise, it returns false.
E next( ) Returns the next element available in the collection.

Let's consider an example program to illustrate accessing elements of a collection via the Iterator.

Example
import java.util.*;

public class TreeSetExample {

	public static void main(String[] args) {

		TreeSet set = new TreeSet();	
		
		Random num = new Random();		
		for(int i = 0; i < 10; i++)
			set.add(num.nextInt(100));
		
		Iterator collection = set.iterator(); 
		
		System.out.println("All the elements of TreeSet collection:");
		while(collection.hasNext())
			System.out.print(collection.next() + ", ");
		
	}
}

Collectionin java

Accessing a collection using for-each

We can use the for-each statement to access elements of a collection.

Let's consider an example program to illustrate accessing items from a collection using a for-each statement.

Example
import java.util.*;

public class TreeSetExample {

	public static void main(String[] args) {

		TreeSet set = new TreeSet();	
		ArrayList list = new ArrayList();
		PriorityQueue queue = new PriorityQueue();
		
		Random num = new Random();		
		for(int i = 0; i < 10; i++) {
			set.add(num.nextInt(100));
			list.add(num.nextInt(100));
			queue.add(num.nextInt(100));
		}
		

		System.out.println("\nAll the elements of TreeSet collection:");		
		for(Object element:set) {
			System.out.print(element + ", ");
		}

		System.out.println("\n\nAll the elements of ArrayList collection:");		
		for(Object element:list) {
			System.out.print(element + ", ");
		}

		System.out.println("\n\nAll the elements of PriorityQueue collection:");		
		for(Object element:queue) {
			System.out.print(element + ", ");
		}
		
	}
}

Collections in java

The For-Each alternative

Using for-each, we can access the elements of a collection. But for-each can only be used if we don't want to modify the contents of a collection, and we don't want any reverse access.

Alternatively, we can use the Iterator to access or cycle through a collection of elements.

Let's consider an example program to illustrate The for-each alternative.

Example
import java.util.*;

public class TreeSetExample {

	public static void main(String[] args) {

		ArrayList list = new ArrayList();
		PriorityQueue queue = new PriorityQueue();
		
		Random num = new Random();		
		for(int i = 0; i < 10; i++) {
			list.add(num.nextInt(100));
			queue.add(num.nextInt(100));
		}

		// Accessing using for-each statement
		System.out.println("\n\nAll the elements of ArrayList collection:");		
		for(Object element:list) {
			System.out.print(element + ", ");
		}

		// Accessing using Iterator
		Iterator collection = queue.iterator();
		System.out.println("\n\nAll the elements of PriorityQueue collection:");		
		while(collection.hasNext()) {
			System.out.print(collection.next() + ", ");
		}
		
	}
}

for-each alternative in java

Accessing elements using ListIterator

The ListIterator interface is used to traverse through a list in both forward and backward directions. It does not support all types of collections. It supports only the collection which implements the List interface.

The ListIterator provides the following methods to traverse through a list of elements.

Method Description
ListIterator listIterator( ) Used obtain an iterator of the list collection.
boolean hasNext( ) Returns true if the list collection has next element, otherwise it returns false.
E next( ) Returns the next element available in the list collection.
boolean hasPrevious( ) Returns true if the list collection has previous element, otherwise it returns false.
E previous( ) Returns the previous element available in the list collection.
int nextIndex( ) Returns the index of the next element. If there is no next element, returns the size of the list.
E previousIndex( ) Returns the index of the previous element. If there is no previous element, returns -1.

Let's consider an example program to illustrate ListIterator to access elements of a list.

Example
import java.util.*;

public class TreeSetExample {

	public static void main(String[] args) {

		ArrayList list = new ArrayList();
		
		Random num = new Random();		
		for(int i = 0; i < 10; i++) {
			list.add(num.nextInt(100));
		}

		// Accessing in forward direction using ListIterator
		ListIterator collection = list.listIterator();
		System.out.println("\n\nAll the elements of PriorityQueue collection:");		
		while(collection.hasNext()) {
			System.out.print(collection.next() + ", ");
		}

		// Accessing in reverse direction using ListIterator
		System.out.println("\n\neverse of the PriorityQueue collection:");		
		while(collection.hasPrevious()) {
			System.out.print(collection.previous() + ", ");
		}
		
	}
}

ListIterator interface in java