一、集合中的迭代器
1.创建迭代器遍历有2步
- 创建迭代对象
开始迭代
//创建集合对象Collection c = new ArrayList();c.add("add");c.add("wqe");//创建迭代器对象Iterator it = c.iterator();//开始迭代while (it.hasNext()){Object obj = it.next();System.out.println(obj);}/*if(it.hasNext()){Object obj = it.next();System.out.println();}*/
2.HashSet集合
- 无序
不重复
//HashSet 集合:无序不可重复Collection c2 = new HashSet();c2.add(100);c2.add(200);c2.add(50);c2.add(60);c2.add(900);Iterator it2 = c2.iterator();while (it2.hasNext()){System.out.println(it2.next());}
二、Collection方法
1.深入Collection集合的contains方法
contains底层调用了equals方法,String中重写了equals方法,比较的是内容
package collection;import java.util.ArrayList;import java.util.Collection;public class Test02 {public static void main(String[] args) {Collection c = new ArrayList();String s1 = new String("abc");c.add(s1);System.out.println("元素的个数是:"+c.size());String x = new String("abc");System.out.println(c.contains(x));//true}}
