集合框架之Collection接口中的方法详解:
(一) Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。
(二)Collection的功能概述:
1:添加功能
boolean add(Object obj):添加一个元素
boolean addAll(Collection c):添加一个集合的元素
2:删除功能
void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
3:判断功能
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
boolean isEmpty():判断集合是否为空
4:获取功能
Iterator<E> iterator()
5:长度功能
int size():元素的个数
面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
6:交集功能
boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
7:把集合转换为数组
Object[] toArray()
1 import java.util.ArrayList; 2 import java.util.Collection; 3 /* 4 * Collection集合方法测试(1): 5 * 6 */ 7 public class CollectionDemo1 { 8 public static void main(String[] args) { 9 10 //接口不能实例化,new了一个List接口所实现的ArrayList类 11 Collection c = new ArrayList(); 12 13 // boolean add(Object obj):添加一个元素 14 System.out.println("add:"+c.add("hello")); 15 c.add("hello"); 16 c.add("world"); 17 c.add("java"); 18 19 // void clear():移除所有元素 20 c.clear(); 21 22 // boolean remove(Object o):移除一个元素 23 System.out.println("remove:" + c.remove("hello")); 24 System.out.println("remove:" + c.remove("javaee")); 25 26 // boolean contains(Object o):判断集合中是否包含指定的元素 27 System.out.println("contains:"+c.contains("hello")); 28 System.out.println("contains:"+c.contains("android")); 29 30 // boolean isEmpty():判断集合是否为空 31 System.out.println("isEmpty:"+c.isEmpty()); 32 33 //int size():元素的个数 34 System.out.println("size:"+c.size()); 35 36 System.out.println("c:" + c); 37 } 38 }
1 import java.util.ArrayList; 2 import java.util.Collection; 3 4 /* 5 * Collection集合方法测试(2): 6 * boolean addAll(Collection c):添加一个集合的元素 7 * boolean removeAll(Collection c):移除一个集合的元素 8 * boolean containsAll(Collection c):判断集合中是否包含指定的集合元素
9 * boolean retainAll(Collection c):A对B做交集,最终的结果保存在A中,B不变。返回值表示的是A是否发生过改变。 10 */ 11 public class CollectionDemo2 { 12 public static void main(String[] args) { 13 // 创建集合1 14 Collection c1 = new ArrayList(); 15 c1.add("abc1"); 16 c1.add("abc2"); 17 c1.add("abc3"); 18 c1.add("abc4"); 19 20 // 创建集合2 21 Collection c2 = new ArrayList(); 22 // c2.add("abc1"); 23 // c2.add("abc2"); 24 // c2.add("abc3"); 25 // c2.add("abc4"); 26 c2.add("abc5"); 27 c2.add("abc6"); 28 c2.add("abc7"); 29 30 // boolean addAll(Collection c):添加一个集合的元素 31 // System.out.println("addAll:" + c1.addAll(c2)); 32 33 //boolean removeAll(Collection c):移除一个集合的元素
34 //只要有一个元素被移除了,就返回true。 35 //System.out.println("removeAll:"+c1.removeAll(c2)); 36 37 //boolean containsAll(Collection c):判断集合中是否包含指定的集合元素
38 //只有包含所有的元素,才叫包含 39 // System.out.println("containsAll:"+c1.containsAll(c2)); 40 41 //boolean retainAll(Collection c):
42 //假设有两个集合A,B。 43 //A对B做交集,最终的结果保存在A中,B不变。 44 //返回值表示的是A是否发生过改变。 45 System.out.println("retainAll:"+c1.retainAll(c2)); 46 47 System.out.println("c1:" + c1); 48 System.out.println("c2:" + c2); 49 } 50 }
(三)Collection集合的遍历
(1)集合转化成数组(toArray()方法):
1 import java.util.ArrayList; 2 import java.util.Collection; 3 4 /* 5 * 集合的遍历。其实就是依次获取集合中的每一个元素。 6 * 7 * Object[] toArray():把集合转成数组,可以实现集合的遍历 8 */ 9 public class CollectionDemo3 { 10 public static void main(String[] args) { 11 // 创建集合对象 12 Collection c = new ArrayList(); 13 14 // 添加元素 15 c.add("hello"); // Object obj = "hello"; 向上转型 16 c.add("world"); 17 c.add("java"); 18 19 // 遍历 20 // Object[] toArray():把集合转成数组,可以实现集合的遍历 21 Object[] objs = c.toArray(); 22 for (int x = 0; x < objs.length; x++) { 23 // 注意:数组中没有length()方法,只有length属性 24 // 向下转型 25 String s = (String) objs[x]; 26 System.out.println(s + "---" + s.length()); 27 } 28 } 29 }
(2)迭代器--集合的专用遍历方式(Iterator接口):
Iterator接口:对 collection 进行迭代的迭代器
该接口中的方法有三个:
boolean hasNext() 如果仍有元素可以迭代,则返回 true。
E next() 返回迭代的下一个元素。
void remove() 从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.Iterator; 4 5 /* 6 * Iterator iterator():迭代器,集合的专用遍历方式 7 * Object next():获取元素,并移动到下一个位置。 8 * NoSuchElementException:没有这样的元素,因为你已经找到最后了。 9 * boolean hasNext():如果仍有元素可以迭代,则返回 true。 10 * 11 * 注意:尽量不要多次使用it.next()方法,因为每次使用都是访问一个对象 12 */ 13 public class IteratorDemo { 14 public static void main(String[] args) { 15 // 创建集合对象 16 Collection c = new ArrayList(); 17 18 // 创建并添加元素 19 c.add("hello"); 20 c.add("world"); 21 c.add("java"); 22 23 // Iterator iterator():迭代器,集合的专用遍历方式 24 Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态 25 26 while (it.hasNext()) { 27 // System.out.println(it.next()); 28 String s = (String) it.next(); 29 System.out.println(s); 30 } 31 //用for循环遍历 32 // for(Iterator it = c.iterator();it.hasNext();){ 33 // String s = (String) it.next(); 34 // System.out.println(s); 35 // } 36 } 37 }
(四)关于迭代器Iterator的源码(多层继承):
1 //接口中的方法默认抽象 public abstract 2 //Iterator接口及其中的方法 3 public interface Interator { 4 boolean hasNext(); 5 Object next(); 6 } 7 //Iterable接口 :超级接口,在接口中定义了一个Interator()方法 返回值类型为Iterator接口 8 public interface Iterable { 9 Iterator iterator(); 10 } 11 //Collectio接口继承了Iterable接口同样有了iterator()方法 ,未实现 12 public interface Collection extends Iterable { 13 Iterator iterator(); 14 } 15 //List接口继承了Collectio接口口同样有了iterator()方法,未实现 16 public interface List extends Collection { 17 Iterator iterator(); 18 } 19 //ArrayList类实现了List接口,重写了iterator方法,返回一个Itr类的对象 20 //在Itr类中,该类实现了Iterator接口并重写 了hasNext和next方法 21 public class ArrayList implements List { 22 public Iterator iterator() { 23 return new Itr(); 24 } 25 26 private class Itr implements Iterator { 27 public boolean hasNext() {} 28 public Object next(){} 29 } 30 } 31 32 //这里实际new的是Itr()对象 ; 33 Iterator it = c.iterator();