Estou utilizando a interface interable e estou com dúvida na parte do código -> employeeList.iterator(); -> Aqui ele está usando o iterator() da classe que implementa o List ?
public class Employee {
private String name;
private Integer age;
public Employee(String name, Integer age) {
this.name = name;
this.age = age;
}
}
//IterableDepartment.java which implements Iterable<Employee>
package com.javabrahman.corejava;
import java.util.List;
import java.util.Iterator;
public class IterableDepartment implements Iterable<Employee> {
List<Employee> employeeList;
public IterableDepartment(List<Employee> employeeList){
this.employeeList=employeeList;
}
@Override
public Iterator<Employee> iterator() {
return **employeeList.iterator();**
}
}
//Client class IterableDeptClient.java
//Iterates through IterableDepartment's employees using for-each loop
package com.javabrahman.corejava;
import java.util.Arrays;
import java.util.List;
public class IterableDeptClient {
public static void main(String args[]){
List<Employee> employeeList
= Arrays.asList(new Employee("Tom Jones", 45),
new Employee("Harry Jones", 42),
new Employee("Ethan Hardy", 65),
new Employee("Nancy Smith", 22),
new Employee("Deborah Sprightly", 29));
IterableDepartment iterableDepartment=new IterableDepartment(employeeList);
for(Employee emp: iterableDepartment){
System.out.println(emp.getName());
}
}
}