12. java.util.Collection

来源:互联网 发布:晨曦自动刷单软件 编辑:程序博客网 时间:2024/06/11 10:18

java.util.Collection(不同于java.util.Collections是一个工具类,之后讲) 是一个集合接口,与数组相对应
数组的特点是:长度固定;在一个数组中只能存储一种数据类型
集合的特点是:长度可变;在一个集合中只能存储多种数据类型

在Collection 接口下,有两个常用接口,分别是List, Set, Map,这里讲List和Set
List接口下有三个常见的类,分别是:ArrayList, LinkedList, Vector
Vector 的特点是线程安全,而其它两个类不具备。
ArrayList 的特点是 查询快,但是增删慢。
LinkedList 的特点是 查询慢,但是增删快。
Set接口下有两个常见的类,分别是:HashSet,TreeSet
HashSet 底层数据结构是哈希表,特点是:存取速度快
TreeSet 底层数据结构是二叉树, 特点是:有序的存放(如果存放的元素可以排序的话)

boolean     add(E e)Ensures that this collection contains the specified element (optional operation).boolean     addAll(Collection<? extends E> c)Adds all of the elements in the specified collection to this collection (optional operation).void    clear()Removes all of the elements from this collection (optional operation).boolean     contains(Object o)Returns true if this collection contains the specified element.boolean     isEmpty()Returns true if this collection contains no elements.Iterator<E>     iterator()Returns an iterator over the elements in this collection.boolean     remove(Object o)Removes a single instance of the specified element from this collection, if it is present (optional operation).boolean     removeAll(Collection<?> c)Removes all of this collection's elements that are also contained in the specified collection (optional operation).boolean     retainAll(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).int     size()Returns the number of elements in this collection.Object[]    toArray()Returns an array containing all of the elements in this collection.
import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class LianXi {    public static void main(String[] args) throws Exception{        t_1();    }    public static void t_1() {        Collection c = new ArrayList();        Collection cc = new ArrayList();        Collection ccc = new ArrayList();        c.add("a");        c.add("b");        cc.add(1);        cc.add(2);        cc.add(new A());        cc.add(new B());        boolean b = cc.addAll(c);        System.out.println(c + ", the number is: " + c.size());     // [a, b], the number is: 2        System.out.println("Add all? " + b);        // Add all? true        System.out.println(cc + ", the number is: " + cc.size());        // [1, 2, test.A@65b54208, test.B@1be6f5c3, a, b], the number is: 6        System.out.println(cc.remove(2));       // true        System.out.println(cc.contains(1));     // true        System.out.println(cc.containsAll(c));      // true        System.out.println(cc + ", the number is: " + cc.size());        // [1, test.A@65b54208, test.B@1be6f5c3, a, b], the number is: 5        System.out.println(ccc.isEmpty());      // true        cc.removeAll(c);        System.out.println(cc + ", the number is: " + cc.size());        // [1, test.A@65b54208, test.B@1be6f5c3], the number is: 3      除去cc中的c元素        cc.addAll(c);        cc.retainAll(c);        System.out.println(cc + ", the number is: " + cc.size());        // [a, b], the number is: 2         // 使cc中只保留cc和c的交集元素        c.clear();        System.out.println(c.isEmpty());        // true        Iterator it = cc.iterator();        while (it.hasNext()) {            System.out.print(it.next() + " ");      // a b        }        System.out.println();        Object[] o =  cc.toArray();       // 列表转数组        for (int i = 0; i < o.length(); i++) {            System.out.print(o[i] + " ");       // a b        }    }}class A{}class B{}