🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The Iterable interface in Java represents a collection of objects that can be iterated over. It is used as a foundation for collections to allow for enhanced for-loops and iteration.
Table of Contents
- What is
Iterable? - Implementing
Iterable - Common Methods
- Examples of
Iterable - Conclusion
1. What is Iterable?
Iterable is an interface that allows objects to be the target of the "for-each loop." It provides a standard way to iterate over a collection of elements.
2. Implementing Iterable
To implement Iterable, a class must:
- Implement the
Iterable<T>interface. - Override the
iterator()method to return anIterator<T>.
3. Common Methods
iterator(): Returns an iterator over elements of typeT.
4. Examples of Iterable
Example: Implementing Iterable in a Custom Class
This example demonstrates how to implement the Iterable interface in a custom class.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyCollection implements Iterable<Integer> {
private Integer[] items;
private int size;
public MyCollection(Integer[] items) {
this.items = items;
this.size = items.length;
}
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private int index = 0;
@Override
public boolean hasNext() {
return index < size;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return items[index++];
}
};
}
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4, 5};
MyCollection collection = new MyCollection(data);
for (Integer item : collection) {
System.out.println(item);
}
}
}
Output:
1
2
3
4
5
5. Conclusion
The Iterable interface in Java is a fundamental part of the collections framework, allowing objects to be iterated over easily. By implementing Iterable, you can create custom collections that can be used in enhanced for-loops, improving code readability and functionality.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment