2 Collection和 iterator 接口
Collection 接口是List、Set、Queue接口的父接口,该接口里定义的方法可用于操作Set集合、List集合、Queue集合。其中定义的操作集合的常用方法如下:
Ø boolean add(Object obj):该方法用于向集合里添加一个元素。如果集合对象被添加操作改变了,则返回true。
Ø boolean addAll(Collection c):该方法把集合C里的所有元素添加到指定集合里。如果集合对象被添加操作改变了,则返回true。
Ø void clear():清除集合里的所有元素,将集合长度变为0。
Ø boolean contains(Object o):返回集合里是否包含指定元素。
Ø boolean containsAll(Collection c):返回集合里是否包含集合c里的所有元素。
Ø boolean isEmpty():返回集合是否为空。当长度为0时返回true,否则返回false。
Ø Iterator iterator():返回一个Iterator对象,用于遍历集合里的元素。
Ø boolean remove(Object o):从此列表中移除第一次出现的指定元素。
Ø boolean removeAll(Collection c):从集合中删除集合c里包含的所有元素,如果删除一个或一个以上的元素,则该方法返回true。
Ø boolean retainAll(Collection c):从集合中删除集合c里不包含的元素,如果该操作改变了调用该方法的集合,则该方法返回true。
Ø int size():返回集合里元素的个数。
Ø Object[] toArray ():该方法把集合转换成一个数组。
举例1:
public static void main(String[] args) throws Exception {
Collection c = new ArrayList ();
c.add(“abc”) ;
c.add(1) ;
System. out .println(“集合C的长度”+c.size()); 2
c.remove(1);
System. out .println(“集合C的长度”+c.size()); 1
System. out .println(“集合c中是否包含abc”+c.contains(“abc”));
Collection c1 = new Hash Set ();
c1.add(“abc”) ;
c1.add(“abc”) ;
c1.add(1) ;
System. out .println(“集合C1的长度”+c1.size()); 2
System. out .println(“集合C是否包含C1集合:”+ c.containsAll(c1) );
c.removeAll(c1) ;
System. out .println(“集合C的长度”+c.size()); // 0
c.clear();
System. out .println(“集合C的长度”+c.size()); //0
c1.retainAll(c) ;
System. out .println(“集合C1的长度”+c1.size()); //0
}
8.2.1 使用Iterator接口遍历集合元素
遍历集合的元素一是可以使用前面的toArray()方法,将其转换为一个数组,然后再对数组进行遍历输出,而我更建议你们直接使用Iterator接口进行遍历输出。
Iterator接口也是 java 集合框架的成员,但它与Collection、Map集合不一样。Collection、Map集合主要用来盛装对象,而Iterator则主要用来遍历(迭代访问)Collection集合中的元素,Iterator对象也被称为 迭代器 。
Iterator接口隐藏了各种Collection实现类的底层细节,向应用程序提供了遍历Collection集合元素的统一编程接口。
Ø boolean hasNext():如果被迭代的集合元素还没有被遍历,则返回true。
Ø Object next():返回集合里的下一个元素。
Ø void remove():删除集合里上一次next方法返回的元素。
举例1:
public static void main(String[] args) throws Exception{
Set set = new HashSet ();
set.add(“张三”) ;
set.add(“李四”) ;
set.add(“王五”) ;
Iterator iterator = set.iterator();
while (iterator.hasNext()){
if (“张三”.equals(iterator.next())){
iterator. remove ();
}
}
System. out .println(set);
}
当使用Iterator迭代访问Collection集合元素时,Collection集合里的元素不能被改变,只能通过Iterator的remove方法删除上一次next方法返回的集合元素才可以,否则将会引发并发修改异常:java.util.ConcurrentModificationException。
举例2:
public static void main(String[] args) throws Exception{
Set set = new HashSet ();
set.add(“张三”) ;
set.add(“李四”) ;
set.add(“王五”) ;
Iterator iterator = set.iterator();
while (iterator.hasNext()){
if (“张三”.equals(iterator.next())){
set.remove(“张三”);
}
}
System. out .println(set);
}
这一点类似于windows系统中,当我们打开一个文件时,又对这个文件执行了删除操作。Windows总会提示我们文件正在被使用,不能删除一个道理。
8.2.2 使用 foreach 循环遍历集合元素
除了使用Iterator接口迭代访问Collection集合里的元素之外,还可以使用foreach循环迭代访问集合元素更加便捷。
举例1:
public static void main(String[] args) throws Exception{
Set set = new HashSet ();
set.add(“张三”) ;
set.add(“李四”) ;
set.add(“王五”) ;
for (Object obj : set){
System. out .println(obj);
}
}
注意:使用foreach循环来遍历Collection集合时,与Iterator接口一样,迭代对象并不是元素对象本身,而是系统依次把集合元素对象的值赋给了 迭代 变量而已。
而有的时候你会看到很多人是这么写的。为什么这么写呢,这么写有一个好处,你遍历完毕,你的it对象就是一个垃圾了。看个人习惯。
for(Interator it = set.iterator();it.hasNext();){
System.out.println(it.next());
}
这里有这么一个问题,我的ArrayList集合中存放着三个学生对象,我进行输出学生对象时是这么操作的。
Collection c = new ArrayList();
Student stu1,stu2,stu3;
c.add();
Iterator it = c.Iterator();
while(it.hasNext()){
一、
Student s = (Student)it.next();
System.out.println(s.getName()+s.getAge());
二、
System.out.println(((Student)it.next()).getName()+((Student)it.next()).getAge());
}
这两个哪个有问题,问题在哪。
记住,不要多次使用it.next()方法,因为每次使用都是访问一个对象。