您好,欢迎访问一九零五行业门户网

Java集合之Collection具体详解

java集合是java提供的工具包,包含了常用的数据结构:集合、链表、队列、栈、数组、映射等。java集合工具包位置是java.util.*
java集合主要可以划分为4个部分:list列表、set集合、map映射、工具类(iterator迭代器、enumeration枚举类、arrays和collections)。
java集合框架如下图:
由上图可以看到java框架主要是collection和map。
一、collection是一个接口,是一个高度抽象出来的集合,接口包含了基本操作和属性。
        collection包含了list和set两大分支:
1)、list是一个有序的队列,每一个元素都有它的索引,第一个元素的索引值是0。list的实现类有linkedlist、arraylist、vector和stack。
         (1)、linkedlist实现了list接口,允许元素为空,linkedlist提供了额外的get,remove,insert方法,这些操作可以使linkedlist被用作堆栈、队列或双向队列。
           linkedlist并不是线程安全的,如果多个线程同时访问linkedlist,则必须自己实现访问同步,或者另外一种解决方法是在创建list时构造一个同步的list。
         (2)、arraylist  实现了可变大小的数组,允许所有元素包括null,同时arraylist也不是线程安全的。
         (3)、vector类似于arraylist,但vector是线程安全的。
         (4)、stack继承自vector,实现一个后进先出的堆栈。
            vector、arraylis和linkedlist比较:
            (1)vector是线程安全的,arraylist和linkedlist不是线程安全的,但一般不考虑线程安全因素,arraylist和linkedlist效率比较高。
            (2)arraylist和vector是实现了基于动态数组的数据结构而linkedlist是基于链表的数据结构。
            (3)数组和链表的查询,删除等的性能。
2)、set是一个不允许有重复元素的集合。set的实现类有hashset和treeset。hashset依赖于hashmap,实际上是通过hashmap实现的;treeset依赖于treemap,通过treemap来实现的。
二、map是一个映射接口,采用key-value键值对的方式。
        abstractmap是一个抽象类,它实现了map接口中的大部分api,而hashmap、treemap和weakhashmap都是继承于abstractmap,hashtable虽然继承于dictionary,但它实现了map接口。
1)、hashtable
        (1)hashtable继承map接口,实现一个key-value映射的哈希表,任何非空的对象都可作为key或者value。
        (2)添加数据put和取出数据get两个操作的时间开销为常数。
        (3)由于作为key的对象是通过计算其散列函数来确定与之对应的value的位置,因此任何作为key的对象都必须要实现hashcode和equals方法。hashcode和equals方法都是继承自根类object。
        (4)hashtable是线程安全的。
2)、hashmap
        (1)hashmap和hashtable类似,但hashmap是非线程安全的,且允许key和value都为空。
        (2)将hashmap视为collection时,其迭代操作时间开销和hashmap的容量成正比,如果迭代性能的操作相当重要的话,不要将hashmap的初始化容量舍得过高。
3)、treemap
        (1)hashmap通过hashcode对其内容进行快速查找,无序的,而treemap中所有的元素都保持着某种固定的顺序,有序的。
        (2)treemap没有调优选项,因为该树总是处于平衡状态。
4)、weakhashmap
          (1) weakhashmap是一种改进的hashmap,它对key实行“弱引用”,如果一个key不再被外部所引用,那么该key可以被gc回收。
总结
(1)如果涉及到堆栈,队列等操作,应该考虑用list;对于需要快速插入,删除元素,应该使用linkedlist;如果需要快速随机访问元素,应该使用arraylist。
(2)如果程序在单线程环境中,或者访问仅仅在一个线程中进行,考虑非同步的类,其效率较高;如果多个线程可能同时操作一个类,应该使用同步的类。
(3)要特别注意对哈希表的操作,作为key的对象要正确复写equals和hashcode方法。
(4)使用map时,查找、更新、删除、新增最好使用hashmap或hashtable;对map进行自然顺序或自定义键顺序遍历时,最好使用treemap;
(5)尽量返回接口而非实际的类型,如返回list而非arraylist,这样如果以后需要将arraylist换成linkedlist时,客户端代码不用改变。这就是针对抽象编程。
collection接口源码
public interface collection<e> extends iterable<e> { int size(); //大小 boolean isempty();//是否为空 boolean contains(object o); //是否包含某个对象 iterator<e> iterator(); //迭代 object[] toarray(); //转化为数组 <t> t[] toarray(t[] a); boolean add(e e); //增加对象 boolean remove(object o); //删除对象 boolean containsall(collection<?> c); //判断是否包含相同的collection boolean addall(collection<? extends e> c); //将collection追加到 boolean removeall(collection<?> c); //删除所有相同对象 default boolean removeif(predicate<? super e> filter) { objects.requirenonnull(filter); boolean removed = false; final iterator<e> each = iterator(); while (each.hasnext()) { if (filter.test(each.next())) { each.remove(); removed = true; } } return removed; } boolean retainall(collection<?> c); void clear(); boolean equals(object o); int hashcode(); @override default spliterator<e> spliterator() { return spliterators.spliterator(this, 0); } default stream<e> stream() { return streamsupport.stream(spliterator(), false); } default stream<e> parallelstream() { return streamsupport.stream(spliterator(), true); } }
以上就是java集合之collection具体详解的详细内容。
其它类似信息

推荐信息