示例:
package com.github.collection1.demo5;
/**
* @author maxiaoke.com
* @version 1.0
*/
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" + "name='" + this.name + '\'' + ", age=" + this.age + '}';
}
}
示例:
package com.github.collection1.demo5;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* @author maxiaoke.com
* @version 1.0
*/
public class Test {
public static void main(String[] args) {
Collection<Person> collection = new ArrayList<>();
collection.add(new Person("张三", 11));
collection.add(new Person("李四", 59));
collection.add(new Person("王五", 19));
collection.add(new Person("赵六", 42));
collection.add(new Person("田七", 8));
collection.add(new Person("王八", 2));
for (Iterator<Person> iterator = collection.iterator(); iterator.hasNext();) {
Person next = iterator.next();
System.out.println(next);
}
}
}