top of page

Iterator interface in Java - Java coding help

What is an Iterator?

In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterator are fixed, iterators are often implemented in terms of the structures underlying a container implementation and are often tightly coupled to the container to enable the operational semantics of the iterator


What is an iterable interface?

In general, an object Implementing Iterable allows it to be iterated. An iterable interface allows an object to be the target of an enhanced for loop(for each loop).


There are three ways in which objects of Iterable can be iterated.

  1. Using enhanced for loop(for-each loop)

  2. Using Iterable forEach loop

  3. Using Iterator<T> interface

Iterate an Iterable using enhanced for loop:

Objects of Classes implementing Collection interface can be iterated using a for-each loop, Collection interface extends Iterable interface.


Example:

import java.util.ArrayList;
import java.util.*;
class Main {
public static void main (String[] args) {
List<String> list = new ArrayList<String>();
list.add("Coders");
list.add("Arts");
for( String element : list ){
System.out.println( element );
}
}
}

Output:


Coders

Arts


Iterate an Iterable using a forEach loop:

The forEach() method takes the Lambda Expression as a parameter. This Lambda Expression is called for each element of the collection. In the below example, for each element of the list, the function prints the element to the console.


Example:

import java.util.ArrayList;
import java.util.*;
class Main {
public static void main (String[] args) {
List<String> list = new ArrayList<String>();
list.add("Coders");
list.add("Arts");
list.forEach(
(element) -> { System.out.println(element); });
}
}

Output:


Coders

Arts


Iterate an Iterable using Iterator:

We can iterate the elements of Java Iterable by obtaining the Iterator from it using the iterator() method.


The methods used while traversing the collections using Iterator to perform the operations are:

  • hasNext(): It returns false if we have reached the end of the collection, otherwise returns true.

  • next(): Returns the next element in a collection.

  • remove(): Removes the last element returned by the iterator from the collection.

  • forEachRemaining(): Performs the given action for each remaining element in a collection, in sequential order.

Example:

import java.util.ArrayList;
import java.util.*;
class Main {
public static void main (String[] args) {
List<String> list = new ArrayList<String>();
list.add("Coders");
list.add("Arts");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}}}

Output:


Coders

Arts


Methods of Iterable:

  • forEach​(Consumer<? super T> action): Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

  • iterator(): Returns an iterator over elements of type T.

  • spliterator(): Creates a Spliterator over the elements described by this Iterable.

How does CodersArts help you in Java coding?

CodersArts provide :

  • Java assignment Help

  • Help in Java development Projects

  • Mentorship from Experts Live 1:1 session

  • Course and Project completions

  • CourseWork help


bottom of page