Typical for-each construct usage for(MyClass myClassObject: list){ code to use myClassObject } Where list is an instance of . Most of the important in-built collection types now support iteration using the enhanced for-each loop. This is by virtue of their implementing the interface Iterable. In fact Jadeveon Clowney Youth Jersey , any class which implements Iterable, can be used in a for-each loop to iterate over the objects of type T which it holds or encapsulates. Extending this logic to the small code snippet we saw above 鈥?MyCollection which implements Iterable, can be used in a for-each loop to iterate through MyClass objects stored in it. Having understood the relationship between implementing Iterable interface and use of the implementing class in for-each loop Lamar Miller Youth Jersey , let us now understand how to go about implementing Iterable. How to implement Iterable interface Any class implementing Iterable needs to follow three simple steps 鈥? 1. Implement Iterable interface. 2. Override Iterable鈥檚 iterator() method. 3. Return an instance of Iterator from the iterator() method. So, if you have an APIClass containing a collection of String type of elements, and you want clients of this API to be able to access theString objects using a for-each loop DeAndre Hopkins Youth Jersey , then your three steps of implementing Iterable would go like this 鈥? 1. Implement Iterable. 2. Override Iterable鈥檚 iterator() method. 3. Return an instance of Iterator from the iterator() method. Simple, right! There is a small piece of logic missing though!!How do you get hold of an Iterator instance pointing to your stored collection? The general practice in this case is to return the in-built Iterator instance of the collection class you use to store the iterable objects in your API. So, if you use a List to store the String objects to be iterated J.J. Watt Youth Jersey , then you return Iterator returned by ator() method as the output of overridden ator() method. Let us see a Java code example to see how Iterable implementation can be done. Java code example showing Iterable implementation Lets take a simple case of aggregation to show an Iterable implementation in action. For our example scenario we have 2 types 鈥?Department and Employee. A Department instance holds multiple Employee instances in a employee list, or List. We will make Department class implement the Iterable interface. Doing so would would allow us to iterate through employees of a department using the for-each loop just by getting hold of a Department instance. Let us see the code in action now, which will be followed by detailed explanation of the code. Java code example showing Iterable implementation (POJO) package java; public class Employee { private String name; private Integer age;
standard override of equals() & hashcode() methods goes here } which implements Iterable package java;
import ; import ator;
public class IterableDepartment implements Iterable {
List employeeList;
public IterableDepartment(List employeeList){ oyeeList=employeeList; }
@Override public Iterator iterator() { return ator(); } } Client class Iterates through IterableDepartment's employees using for-each loop package java;
import ys; import ;
public class IterableDeptClient { public static void main(String args[]){ List employeeList = st(new Employee("Tom Jones", 45), new Employee("Harry Jones" Kahale Warring Youth Jersey , 42), new Employee("Ethan Hardy", 65) Max Scharping Youth Jersey , new Emplo