首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Iterator迭代器检索List,Set,Map聚合

2012-12-22 
Iterator迭代器检索List,Set,Map集合Iterator:对 collection 进行迭代的迭代器常用方法:boolean hasNext()

Iterator迭代器检索List,Set,Map集合
Iterator:对 collection 进行迭代的迭代器

常用方法:


boolean hasNext()如果仍有元素可以迭代,则返回 true。(换句话说,如果 next 返回了元素而不是抛出异常,则返回 true)。

Object next()返回迭代的下一个元素。

void remove()从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。
每次调用 next 只能调用一次此方法。如果进行迭代时用调用此方法之外的其他方式修改了该迭代器所指向的 collection,则迭代器的行为是不确定的。


Iterator迭代器检索List,Set,Map集合


首先我们需要一个实体类Student类。
package maps;

public class Student {

private Integer sno;
private String sname;
private Double smark;

public Student(){}

public Student(Integer sno,String sname,Double smark)
{
   this.sno=sno;
   this.sname=sname;
   this.smark=smark;
}
public Integer getSno() {
   return sno;
}
public void setSno(Integer sno) {
   this.sno = sno;
}
public String getSname() {
   return sname;
}
public void setSname(String sname) {
   this.sname = sname;
}
public Double getSmark() {
   return smark;
}
public void setSmark(Double smark) {
   this.smark = smark;
}
}

实例一:迭代List列表

public void IteratorList()
{
   //创建实体类对象
   Student stu1=new Student(1,"name1",60.08);
   Student stu2=new Student(2,"name2",70.08);
   Student stu3=new Student(3,"name3",80.08);
   Student stu4=new Student(4,"name4",90.08);
   //创建List列表
   List list=ArrayList();
   //添加元素
   list.add(stu1);
   list.add(stu2);
   list.add(stu3);
   list.add(stu4);
   //获得迭代器
   Iterator it=list.iterator();
   //判断是否还有元素可以迭代
   while(it.hasNext())
   {
    //返回迭代的下一个元素。
    Student st=(Student)it.next();
    //结果输出
    System.out.println(st.getSname());
   }

}

实例一:迭代set列表

public void IteratorSet()
{
   //创建实体类对象
   Student stu1=new Student(1,"name1",60.08);
   Student stu2=new Student(2,"name2",70.08);
   Student stu3=new Student(3,"name3",80.08);
   Student stu4=new Student(4,"name4",90.08);
   //创建List列表
   Set set=HashSet();
   //添加元素
   set.add(stu1);
   set.add(stu2);
   set.add(stu3);
   set.add(stu4);
   //获得迭代器
   Iterator it=set.iterator();
   //判断是否还有元素可以迭代
   while(it.hasNext())
   {
    //返回迭代的下一个元素。
    Student st=(Student)it.next();
    //结果输出
    System.out.println(st.getSname());
   }

}

实例一:迭代Map列表

public void IteratorMap()
{
   //创建实体类对象
   Student stu1=new Student(1,"name1",60.08);
   Student stu2=new Student(2,"name2",70.08);
   Student stu3=new Student(3,"name3",80.08);
   Student stu4=new Student(4,"name4",90.08);
   //创建List列表
   Map map=HashMap();
   //添加元素,以学号为Key,Student实例为值
   map.put(stu1.getSno(), stu1);
   map.put(stu2.getSno(), stu2);
   map.put(stu3.getSno(), stu3);
   map.put(stu4.getSno(), stu4);
  //返回Map中包含的键的 Set 列表。
   Set set=map.keySet();
   //获得Key--Set列表迭代器
   Iterator it=set.iterator();

   while(it.hasNext())
   {
    Object key=it.next();
    //判断key 是否存在
    if(map.containsKey(key))
    {
     //返回迭代的元素
     Student st=(Student)map.get(key);
     //结果输出
     System.out.println(st.getSname());
    }
   }

}

1 楼 lsh4894 2011-05-25   沙发。。。。

热点排行