Iterable Interface in Java Collections
Fast recall
Quick revision
Core answer
Iterable has one abstract method, iterator(), plus default methods such as forEach() and spliterator(). Collection extends it so collection objects can be used in an enhanced for loop. A class may implement Iterable without supporting Collection operations. Arrays also work with enhanced for through a separate language rule.Whether you understand why for-each works and the difference between Iterable and Iterator
Confusing Iterable (the factory) with Iterator (the cursor), or thinking for-each only works on arrays/collections
Show a custom class implementing Iterable to get for-each support without implementing the whole Collection contract
Interview-ready guide
Interview answer
Iterable is the minimum interface an object implements to provide an Iterator and work with Java's enhanced for loop. Its one abstract method is iterator(), and it also has default methods such as forEach and spliterator. The returned Iterator is the stateful cursor with hasNext and next; its remove operation is optional.
Collection extends Iterable, but an Iterable does not need to be a Collection. A lazy sequence can provide traversal without supporting size or mutation. Arrays also work with enhanced for through a separate language rule, not because arrays implement Iterable.
Iterable Interface in Java Collections
Direct Answer
Iterable<T> has one abstract method, iterator(), plus default methods such as forEach() and spliterator(). Collection extends it so collection objects can be used in an enhanced for loop. A class may implement Iterable without supporting Collection operations. Arrays also work with enhanced for through a separate language rule.
Code Example
import java.util.*;
// Generates IDs on the fly - no storage, not a Collection,
// yet works with for-each because it implements Iterable.
class OrderIdRange implements Iterable<String> {
private final int start, end;
OrderIdRange(int start, int end) { this.start = start; this.end = end; }
@Override public Iterator<String> iterator() {
return new Iterator<>() {
private int cur = start;
@Override public boolean hasNext() { return cur < end; }
@Override public String next() {
if (!hasNext()) throw new NoSuchElementException();
return "ORDER-" + (cur++);
}
};
}
public static void main(String[] args) {
for (String id : new OrderIdRange(100, 103)) {
System.out.println(id); // ORDER-100, ORDER-101, ORDER-102
}
}
}
Common Mistakes & Pitfalls
- Iterable vs Iterator confusion.
Iterableis the factory (iterator());Iteratoris the cursor (hasNext/next). You implement Iterable; it hands back an Iterator. - Thinking for-each is only for arrays/collections. Any class implementing
Iterablegets for-each for free. - Implementing Collection when Iterable is enough. A read-only sequence forced to implement Collection must throw
UnsupportedOperationExceptionfromadd/remove— just implement Iterable instead. - Forgetting arrays are a special case. Arrays support for-each via a compiler rule, not by being Iterable.
Check your understanding
Answer these without looking back at the article.
- What are the three methods of Iterator?
- How would you make a custom class work with a for-each loop?
- What is the difference between Iterable and Iterator?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.