Java集合类笔记2:Collection和Iterator
1.Collection是List,Set,和Queue接口的父接口,该接口定定义的方法可适用于List,Map和Queue
TestCollection.java
2.如果想依次访问集合里的每一个元素,则需要某种方式来遍历集合元素,遍历集合元素的方法:
1)使用Iterator接口遍历集合元素:
Iterator也是Java集合框架的成员,但其主要好是用于遍历(即迭代访问)collection集合中的元素,Iterator对象也称为迭代器。
testIerator.javaimport java.util.*;public class testForeach{public static void main(String[] args) {Collection books = new HashSet();books.add(new String("core java"));books.add(new String("thinking in java"));books.add(new String("java程序设计"));for(Object obj : books) {String book = (String)obj;System.out.println(book);book = "测试";}System.out.println(books);}}