In this article, we will understand Iterable interface in java, its meaning, use and methods with example
Iterable interface resides in java.lang package and was added in java 1.5. This interface is only applicable to collection of items such as array, list, map, queue etc., which can be iterated.
Any collection that implements this interface indicates that it can be iterated or you can loop over its elements.

Iterating an iterable
An object that implements Iterable can be iterated in following ways.
1. Using iterator()
Iterable interface has an iterator() method which returns an object of java.util.Iterator interface.
This object can then be used for looping over elements of a collection using its hasNext() and next() methods.
Example,

// create arraylist
ArrayList<String> list = new ArrayList<>();
// add elements
list.add("Google");
list.add("Facebook");
list.add("Amazon");
list.add("Youtube");
// get iterator
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
  System.out.println(iterator.next());
}

java.util.ArrayList extends List interface which extends Collection interface.
Collection, in turn, extends Iterable. Thus, we can invoke iterator() method on an arraylist.
This iterator is used to loop over the elements of arraylist.
Output of above program is

Google
Facebook
Amazon
Youtube

2. Using enhanced for loop
A collection that implements java.lang.Iterable can also be iterated using enhanced for loop. Example,

ArrayList<String> list = new ArrayList<>();
list.add("Google");
list.add("Facebook");
list.add("Amazon");
list.add("Youtube");
// enhanced for loop
for(String item:list) {
  System.out.println(item);
}

With enhanced for loop, the type of loop variable must be same as the type of the elements of collection, which is, String in this case.
Output is

Google
Facebook
Amazon
Youtube

3. Using forEach() method
Java 8 introduced a forEach() method to java.lang.Iterable interface, which iterates over the elements of a collection.
forEach is a default interface method which accepts an argument of type java.util.function.Consumer as argument.
Consumer is a functional interface, which means that you can supply a Lambda expression to forEach(). Example,

ArrayList<String> list = new ArrayList<>();
list.add("Google");
list.add("Facebook");
list.add("Amazon");
list.add("Youtube");
list.forEach(item -> {
  System.out.println(item);
});

This prints

Google
Facebook
Amazon
Youtube

Iterable methods
Following are important methods defined in Iterable interface.
1. iterator()
This method returns an object of Java iterator and is used to loop over the elements of the collection. Example of this method is given above.
2. forEach()
forEach() is a default method added to Iterable in java 8. It accepts a lambda expression as argument.
This lambda expression has a single argument and is invoked for each element of the collection.
Lambda function argument contains the value of current collection element in each iteration.
3. splitIterator()
This method is a default interface method added in java 8 and returns an object of java.util.SplitIterator.
SplitIterator is used to iterate and partition the elements of a collection. It is usually used in parallel operations.

Generics in Iterable
java.lang.Iterable interface is generic, meaning that can be defined for different object types as can be seen from its definition below.

public interface Iterable<T> {

}

where T is the generic type.
So, for a collection of different types, the type of iterator will be different. Example,

List<String> names = new ArrayList<>();
// iterator of type String
Iterator<String> nameIterator = names.iterator();

List<Integer> numbers = new ArrayList<>();
// iterator of type Integer
Iterator<Integer> numberIterator = numbers.iterator();

List<Dog> dogs = new ArrayList<>();
// iterator of type Dog
Iterator<Dog> dogIterator = dogs.iterator();

That is all on Iterable interface in java. Hit the clap if the article was useful.

0