Implement Iterable Interface to work with for-each loops in Java
This post will implement the Iterable interface on a Java class holding a collection of objects to iterate over the collection using the for-each loop.
1. Implementing the Iterator Interface
Before we implement the Iterable interface, first, let’s see what we’ll get by only implementing the Iterator interface on a class holding a collection of objects.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import java.util.Iterator; import java.util.NoSuchElementException; // `Collection` class implements the `java.util.Iterator` interface class Collection<T> implements Iterator<T> { private T[] array; private int i = 0; // Constructor public Collection(T[] array) { this.array = array; } /** * Returns an iterator over the array elements */ public Iterator<T> iterator() { i = 0; // important - reset `i` to 0 return this; } /** * Returns true if the array has more elements. */ @Override public boolean hasNext() { return (i < array.length && array[i] != null); } /** * Returns the next element in the iteration. * @throws NoSuchElementException if the iteration has no more elements */ @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } return array[i++]; } } class Main { public static void main(String[] args) { // an Integer array Integer[] ints = new Integer[] { 1, 2, 3 }; // wrap `Integer` array in our `Collection` class Collection<Integer> collection = new Collection<>(ints); Iterator<Integer> itr = collection.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } // for-each loop won't work here } } |
Output:
1
2
3
We can’t use for-each loop here to iterate over the collection since the Collection class didn’t implement the java.util.Iterable interface. The Collection class object can access the Iterator though since Collection class implements the java.util.Iterator interface.
2. Implementing the Iterable Interface
Now let’s make the Collection class implement the java.util.Iterable interface, enabling it to work with for-each loops. Now we can iterate through the array of objects stored in the Collection class using both Iterator and for-each loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import java.util.Iterator; import java.util.NoSuchElementException; // `Collection` class implements the `java.util.Iterable` interface class Collection<T> implements Iterable<T> { private T[] array; private final int n; // Constructor public Collection(T[] array) { this.array = array; this.n = array.length; } /** * Returns an iterator over elements of type T. */ @Override public Iterator<T> iterator() { return new Iterator<T>() { private int i = 0; /** * Returns true if the array has more elements. */ @Override public boolean hasNext() { return (i < n && array[i] != null); } /** * Returns the next element in the iteration. * @throws NoSuchElementException if the iteration has no * more elements */ @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } return array[i++]; } }; } } class Main { public static void main(String[] args) { // an Integer array Integer[] ints = new Integer[] { 1, 2, 3 }; // wrap the Integer array in our `Collection` class Collection<Integer> collection = new Collection<>(ints); // iterate over the array using an iterator Iterator<Integer> itr = collection.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } // iterate over the array using the for-each loop // This works since the `Collection` class implements `Iterable` interface for (Integer i: collection) { System.out.println(i); } } } |
Output:
1
2
3
1
2
3
That’s all about making the Iterable Interface work with for each loop.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)