Iterable Interface in Java
Last Updated :
14 Dec, 2020
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.
- Using enhanced for loop(for-each loop)
- Using Iterable forEach loop
- 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
import java.io.*;
import java.util.*;
class IterateUsingEnhancedForLoop {
public static void main (String[] args) {
List<String> list = new ArrayList<String>();
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
for ( String element : list ){
System.out.println( element );
}
}
}
|
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
import java.io.*;
import java.util.*;
class IterateUsingforEach {
public static void main(String[] args)
{
List<String> list = new ArrayList<>();
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
list.forEach(
(element) -> { System.out.println(element); });
}
}
|
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
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);
}
}
}
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...