集合框架(Collection)

来源:互联网 发布:为什么wifi网络不稳定 编辑:程序博客网 时间:2024/06/08 04:18

Collection是集合框架体系中的根接口,它有两个常用的子接口,别是List和Set

List:有序(存储顺序和取出顺序一致),可重复。
实现了List接口的子类以及这些子类的区别:

ArrayList:底层数据结构是数组,查询快,增删慢。线程不安全,效率高。Vector:底层数据结构是数组,查询快,增删慢。线程安全,效率低LinkedList:底层数据结构是链表,查询慢,增删快。线程不安全,效率高。

Set:无序(存储数据与取出顺序不一致),数据唯一
实现了Set接口的子类对象以及这些子类的区别:

HashSet:底层数据结构是哈希表(是一个元素为链表的数组),:哈希表底层依赖两个方法:hashCode()和equals()保证元素的唯一性TreeSet:底层数据结构是红黑树(是一个自平衡的二叉树),能对存储的元素自动排序,排序的方式有两种:1,自然排序,让元素自身具备比较性,不如让元素所属的类实现Comparable接口;2,比较器排序,让集合具备比较性,让集合的构造方法接收Comparator的实现类对象

Collection是根接口,里面定义的功能属于共性功能:

1,添加功能boolean add(Object o):添加一个元素boolean addAll(Collection c);添加一个集合的元素2,删除功能void chear();移除所有的元素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();获取元素个数6,交集功能boolean retainAll(Collection c);两个集合是否有相同的元素

Iterator iterator() 迭代器:
集合中专用的遍历方式
Iterator功能:
Object next();获取下一个元素,并移动到下一个位置
boolean hasNext();如果仍有元素可以迭代,就然会true,否则返回false

以ArrayList演示List体系里的基本功能:

    public static void main(String[] args) {        // 常见集合对象        ArrayList<String> array = new ArrayList<String>();        // 常见元素对象,并添加元素        array.add("hello");        array.add("world");        array.add("java");        array.add("javaSE");        // 添加一个集合的元素        ArrayList<String> arry1 = new ArrayList<String>();        arry1.addAll(array);        arry1.add("windows");        // 判断集合中是否包含指定的元素,返回的是一个布尔值        System.out.println("contains=" + array.contains("javaSE"));        // 判断集合中是否包含指定的集合元素        System.out.println("containsAll=" + arry1.containsAll(array));        // 判断集合是否为空        System.out.println("isEmpty=" + arry1.isEmpty());        // 获取长度        System.out.println("size=" + array.size());        System.out.println("size=" + arry1.size());        // 使用Iterator遍历        Iterator<String> it = array.iterator();        while (it.hasNext()) {            System.out.println(it.next());        }        // 使用ListIterator迭代器遍历,并添加元素,和删除元素        ListIterator<String> lit = array.listIterator();        while (lit.hasNext()) {            // 添加元素            lit.add("ni");            lit.add("hao");            System.out.println(lit.next());        }        // 取交集,哪个集合调用retainAll方法,交集就存在哪个集合,返回值表示此集合是否改变过        System.out.println("retainAll=" + array.retainAll(arry1));        // 把集合转为数组        Object[] s = array.toArray();        System.out.println(array);        //删除功能        System.out.println("remove="+array.remove("hello"));//移除一个元素        System.out.println("removeAll="+arry1.removeAll(array));        //只要有一个元素被删除就返回true    }

Vector的特点:底层数据结构是数组,查询快,增删慢,线程安全,效率低
特有功能:
a:添加
public void addElement(E obj) – add()
b:获取
public E elementAt(int index) – get()
public Enumeration elements() – iterator()
LinkedList的特点:底层数据结构是链表,查询慢,增删快,线程不安全,效率高
特有功能:
a:添加
addFirst()
addLast()
b:删除
removeFirst()
removeLast()
c:获取
getFirst()
getLast()

0 0