Java Collections Framework

Java Collections Framework

1.       主要接口

2.       主要实现

JDK

Interface

Implementation

Heap

Hash Table

Resizable Array

Balanced Tree

Linked List

Hash Table + Linked List

Legacy

 

Special

1.4

Set

 

HashSet

 

 

 

LinkedHashSet

Vector

 

1.4

SortedSet

 

 

 

TreeSet

 

 

 

 

1.4

List

 

 

ArrayList

 

LinkedList

 

 

 

1.4

Map

 

HashMap

 

 

 

LinkedHashMap

Hashtable

WeakHashMap

IdentityHashMap

1.4

SortedMap

 

 

 

TreeMap

 

 

 

 

1.5

ConcurrentMap

 

ConcurrentHashMap

 

 

 

 

 

 

1.5

Queue

AbstractQueue

PriorityQueue

 

 

 

ConcurrentLinkedQueue

 

 

 

1.5

BlockingQueue

PriorityBlockingQueue

DelayQueue

SynchronousQueue

 

ArrayBlockingQueue

 

LinkedBlockingQueue

 

 

 

 

3.       主要属性

 

接口还是实现

是否同步

是否包含重复数据

是否允许Null

是否有序

备注

Collection

Interface

 

 

 

 

一组数集合

Set

I

 

 

 

相当于数学上的集合,不能有重复数据

HashSet 

Imp

 

hash表存储,性能最好,但是iteration的顺序不保证

TreeSet 

Imp

 

 

按照值的大小进行tree型(red-black tree)存储,效率最慢

LinkedHashSet

Imp

 

 

Hash表+linked list,按照插入set的顺序排列,可以保证顺序,效率仅次于HashSet

SortedSet

I

 

 

自然升序的Set

List

I

 

 

一个有序的集合

ArrayList

Imp

一般来讲效率最好,对于随意访问,效率好
        
        

允许null值,大体等于Vector,只是unsynchronized

LinkedList

Imp

对于随意插入效率好 unsynchronized

Vector

Imp

Synchronized

Queue

I

 

 

有序,可以包含重复数据,一般FIFO

Map

I

 

 

 

 

key-value对集合,不能有重复数据,1个key最多一个值

HashMap

Imp

Hashtable大致相同,除了unsynchronized和允许Null

LinkedHashMap

Imp

 

Hashtable

Imp

Synchronized,不允许Null

SortedMap

I

 

 

 

 

自然升序的Map

 

 

4.       Iterator

public interface Iterator<E> {
    
    
    boolean hasNext();
    
    
    E next();
    
    
    void remove(); //optional
    
    
}
    
    

a)         最方便的删除

static void filter(Collection<?> c) {
    
    
    for (Iterator<?> it = c.iterator(); it.hasNext(); )
    
    
        if (!cond(it.next()))
    
    
            it.remove();
    
    
}
    
    

b)        fail-fast

The iterators returned by the this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

 

c)        ListIteratorn+1index

 

 

5.       同步处理

Vector和Hashtable外,其余均为unsynchronized。以ArrayList为例子,如果在多thread情况下使用ArrayList,如果有其他线程修改了ArrayList的结构(修改值可以),必须自己进行手工同步操作。
    
    

 

List list = Collections.synchronizedList(new ArrayList());
    
    
      ...
    
    
  synchronized(list) {
    
    
      Iterator i = list.iterator(); // Must be in synchronized block
    
    
      while (i.hasNext())
    
    
          foo(i.next());
    
    
  }
    
    
      相应的其他方法:
    
    

static Collection

synchronizedCollection(Collection c)
          Returns a synchronized (thread-safe) collection backed by the specified collection.

static List

synchronizedList(List list)
          Returns a synchronized (thread-safe) list backed by the specified list.

static Map

synchronizedMap(Map m)
          Returns a synchronized (thread-safe) map backed by the specified map.

static Set

synchronizedSet(Set s)
          Returns a synchronized (thread-safe) set backed by the specified set.

static SortedMap

synchronizedSortedMap(SortedMap m)
          Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

static SortedSet

synchronizedSortedSet(SortedSet s)
          Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

      
  
  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值