最近使用Hibernate,在多对一映射中需要用到Set<T> 对象,为了把对象中的数据输出,用到了Iterator对象。
Iterator是一个轻量级(创建代价很小)对象,它可以遍历序列中数据,无需知道其序列底层结构。
一、Iterator对象的创建
(
Set<Product> products;
......
)
Iterator it=products.iterator();
二、遍历
(1)while遍历
while(it.hasNext()){
Product product=it.next();
(2)for遍历
for(Iterator it=products.iterator();it.hasNext()){
Product product=it.next();