java-API之集合3——Collection接口
集合Collection接口
Collection 层次结构 中的根接口。一些 collection (List)允许有重复的元素,有序的,而另一些则(Set)不允许有重复元素,则是无序的。参考源码:
集合Collection的共有方法
- 添加元素
- 删除元素
boolean remove(Object o)从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。booleanremoveAll(Collection<?> c)移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。boolean remove(Object o)从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。boolean retainAll(Collection<?> c)仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。void clear()移除此 collection 中的所有元素(可选操作)。
- 其它
boolean contains(Object o)如果此 collection 包含指定的元素,则返回 true。boolean containsAll(Collection<?> c)如果此 collection 包含指定 collection 中的所有元素,则返回 true。boolean equals(Object o)比较此 collection 与指定对象是否相等。int hashCode()返回此 collection 的哈希码值。boolean isEmpty()如果此 collection 不包含元素,则返回 true。int size()返回此 collection 中的元素数。Object[] toArray()返回包含此 collection 中所有元素的数组。返回在此 collection 的元素上进行迭代的迭代器。Iterator<E>iterator()
集合Collection共有方法的测试
1 public class Test_Collection { 2 3 public static void main(String[] args) { 4 // 1、创建对象 5 // <Integer>就约束了list 集合中元素的类型必须是整型 6 Collection<Integer> list = new ArrayList<Integer>(); 7
8 // 2、常用方法 9 list.add(100); 10 System.out.println(list); // [100] 11 System.out.println(list.contains(100)); // true 是否包含指定元素 12 System.out.println(list.contains(101)); // false 13 // list.add("abc"); // 类型不匹配 14 15 System.out.println(list.equals(list)); // true 16 Collection<String> list2 = new ArrayList<String>(); // 17 System.out.println(list.equals(list2)); // false 判断是否相等 18 19 System.out.println(list.hashCode()); // 131 返回哈希码值 20 System.out.println(list.isEmpty()); // false 判断集合是否为空 21 System.out.println(list2.isEmpty()); // true 22 23 list.remove(100); // 移除集合中指定的元素 24 System.out.println(list); // [] 25 list.add(200); 26 System.out.println(list.size()); // 1 集合的大小 27 System.out.println(list2.size()); // 0 28 list.add(100); 29 System.out.println(list.toArray()); // [Ljava.lang.Object;@15db9742 将集合中的元素转换成数组 30 System.out.println(Arrays.toString(list.toArray())); // [200, 100] 31
32 33 // 集合和集合的操作 34 Collection<Integer> list3 = new ArrayList<Integer>(); 35 list3.add(1); 36 list3.add(2); 37 list3.add(3);
38 System.out.println(list.containsAll(list3)); //false 判断集合中是否包含参数集合中的所有元素 39 list.addAll(list3); // 添加参数集合中的所有元素 40 System.out.println(list); // [200, 100, 1, 2, 3] 41 System.out.println(list.containsAll(list3)); // true 42 list.removeAll(list3); // 移除集合中参数集合中所有的元素 43 System.out.println(list); // [200, 100] 44 list.retainAll(list3); // 保留集合中参数集合中的所有元素 45 System.out.println(list); // [1, 2, 3] 46 47 }
Collection中的迭代器Iterator
所谓迭代,就是循环,也是遍历,下面我们格外测试一下最后一个迭代器Iterator方法。
iterator常用方法:
boolean hasNext()如果仍有元素可以迭代,则返回 true。E next()返回迭代的下一个元素。void remove()从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。
1 public class Test4_List { 2 3 public static void main(String[] args) { 4 // 1、创建List对象 5 List<Integer> list = new ArrayList<Integer>(); 6 //List特有 特有的 7 list.add(1); 8 list.add(2); 9 list.add(4); 10 list.add(3); 11 list.add(5); 12 list.add(9); 13 14 // 第一种方式 迭代器iterator 15 Iterator it = list.iterator(); 16 while( it.hasNext() ) { 17 System.out.println(it.next()); // 遍历 18 } 19 System.out.println(); 20 21 // 第二种方式 高效for循环——foreach 22 for(Integer i : list) { 23 System.out.println(i); // 遍历 24 } 25 26 // 第三种方式 普通for循环 27 for (int i = 0; i < list.size(); i++) { 28 System.out.println(list.get(i)); // 遍历 效率略低 29 } 30 System.out.println(); 31 32 } 33 }

浙公网安备 33010602011771号