ITERABLE IN JAVA
Definition: Iterable is an interface in Java that allows objects to be traversed.
Package: [Link]
Syntax: public interface Iterable<T>
Main Method: Iterator<T> iterator()
Purpose:
- Enables for-each loop
- Used to traverse collections
Example (For-each loop):
List<String> names = new ArrayList<>();
[Link]("Sanjay");
[Link]("Arun");
for(String name : names){
[Link](name);
}
Iterator Example:
Iterator<Integer> it = [Link]();
while([Link]()){
[Link]([Link]());
}
Important Points:
- Root interface of collections
- Only one method: iterator()
- Used for sequential access