Friday, December 27, 2024
Google search engine
HomeLanguagesJavaIterable Interface in Java

Iterable Interface in Java

The Iterable interface was introduced in JDK 1.5. It belongs to java.lang package. In general, an object Implementing Iterable allows it to be iterated. An iterable interface allows an object to be the target of enhanced for loop(for-each loop). 

Definition of Iterable

public interface Iterable<T>
{
  Iterator<T>    iterator();
  
  Spliterator<T> spliterator();

  void           forEach(Consumer<? super T> action);
}

Here, T is the type of element returned by the Iterator.

Ways of Iterating

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 for-each loop, Collection interface extends Iterable interface.

Java




// Java Program to demonstrate iterate 
// an iterable using for-each loop
  
import java.io.*;
import java.util.*;
  
class IterateUsingEnhancedForLoop {
    public static void main (String[] args) {
          
      // create a list
      List<String> list = new ArrayList<String>();
  
      // add elements
      list.add("Geeks");
      list.add("for");
      list.add("Geeks");
  
      // Iterate through the list
      for( String element : list ){
          System.out.println( element );
      }
    }
}


Output

Geeks
for
Geeks

Iterate an Iterable using 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.

Java




// Java Program to demonstrate iterate
// an Iterable using forEach method
  
import java.io.*;
import java.util.*;
  
class IterateUsingforEach {
    public static void main(String[] args)
    {
          // create a list
        List<String> list = new ArrayList<>();
  
          // add elements to the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
  
          // Iterate through the list
        list.forEach(
            (element) -> { System.out.println(element); });
    }
}


Output

Geeks
for
Geeks

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.

Java




// Java Program to demonstrate iterate
// an Iterable using an Iterator
  
import java.io.*;
import java.util.*;
  
class IterateUsingIterator {
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<>();
  
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
  
        Iterator<String> iterator = list.iterator();
  
        while (iterator.hasNext()) {
            String element = iterator.next();
            System.out.println(element);
        }
    }
}


Output

Geeks
for
Geeks

Methods of Iterable

METHOD

DESCRIPTION

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.

Must Read

Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Iterable.html

RELATED ARTICLES

Most Popular

Recent Comments