ArrayList,LinkedList,Vector 以及HashMap,HashTable的关系和区别<转载>

ArrayList,LinkedList,Vestor这三个类都实现了java.util.List接口,但它们有各自不同的特性,主要如下:

一、同步性

ArrayList,LinkedList是不同步的,而Vestor是的。所以如果要求线程安全的话,可以使用ArrayList或LinkedList,可以节省为同步而耗费开销。但在多线程的情况下,有时候就不得不使用Vector了。当然,也可以通过一些办法包装ArrayList,LinkedList,使他们也达到同步,但效率可能会有所降低。

二、数据增长
从内部实现机制来讲ArrayList和Vector都是使用Objec的数组形式来存储的。当你向这两种类型中增加元素的时候,如果元素的数目超出了内部数组目前的长度它们都需要扩展内部数组的长度,Vector缺省情况下自动增长原来一倍的数组长度,ArrayList是原来的50%,所以最后你获得的这个集合所占的空间总是比你实际需要的要大。所以如果你要在集合中保存大量的数据那么使用Vector有一些优势,因为你可以通过设置集合的初始化大小来避免不必要的资源开销。

三、检索、插入、删除对象的效率

ArrayList和Vector中,从指定的位置(用index)检索一个对象,或在集合的末尾插入、删除一个对象的时间是一样的,可表示为O(1)。但是,如果在集合的其他位置增加或移除元素那么花费的时间会呈线形增长:O(n-i),其中n代表集合中元素的个数,i代表元素增加或移除元素的索引位置。为什么会这样呢?以为在进行上述操作的时候集合中第i和第i个元素之后的所有元素都要执行(n-i)个对象的位移操作。

LinkedList中,在插入、删除集合中任何位置的元素所花费的时间都是一样的—O(1),但它在索引一个元素的时候比较慢,为O(i),其中i是索引的位置。

所以,如果只是查找特定位置的元素或只在集合的末端增加、移除元素,那么使用Vector或ArrayList都可以。如果是对其它指定位置的插入、删除操作,最好选择LinkedList

=======================================================================================================================================================

HashMap,HashTable的区别

HashMap 和 HashTable 都是通过链接法解决碰撞

  1、 继承和实现区别

  Hashtable是基于陈旧的Dictionary类,完成了Map接口;HashMap是Java 1.2引进的Map接口的一个实现(HashMap继承于AbstractMap,AbstractMap完成了Map接口)。

  2、 线程安全不同

  HashTable的方法是同步的,HashMap是未同步,所以在多线程场合要手动同步HashMap。

  3、 对null的处理不同

  HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。即HashTable不允许null值其实在编译期不会有任何的不一样,会照样执行,只是在运行期的时候Hashtable中设置的话回出现空指针异常。HashMap允许null值是指可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。

  4、 方法不同

  HashTable有一个contains(Object value),功能和containsValue(Object value)功能一样。

  5、HashTable使用Enumeration,HashMap使用Iterator。

  6、HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数。

  7、哈希值的使用不同,HashTable直接使用对象的hashCode,代码是这样的:

  int hash = key.hashCode();

  int index = (hash & 0x7FFFFFFF) % tab.length;

  而HashMap重新计算hash值,而且用与代替求模:

  int hash = hash(k);

  int i = indexFor(hash, table.length);

  static int hash(Object x) {

  int h = x.hashCode();

  h += ~(h << 9);

  h ^= (h >>> 14);

  h += (h << 4);

  h ^= (h >>> 10);

  return h;

  }

  static int indexFor(int h, int length) {

  return h & (length-1);

  }

 

区别

Hashtable

Hashmap

继承、实现

Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable,Serializable

HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable,Serializable

 

线程同步

已经同步过的可以安全使用

未同步的,可以使用Colletcions进行同步Map Collections.synchronizedMap(Map m)

对null的处理

 

Hashtable table = new Hashtable();

table.put(null, "Null");

table.put("Null", null);

table.contains(null);

table.containsKey(null);

table.containsValue(null);

后面的5句话在编译的时候不会有异常,可在运行的时候会报空指针异常具体原因可以查看源代码

public synchronized V put(K key, V value) {

// Make sure the value is not null

if (value == null) {

throw new NullPointerException();

}

HashMap map = new HashMap();

map.put(null, "Null");

map.put("Null", null);

map.containsKey(null);

map.containsValue(null);

以上这5条语句无论在编译期,还是在运行期都是没有错误的.

在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。

增长率

protected void rehash() {

int oldCapacity = table.length;

Entry[] oldMap = table;

int newCapacity = oldCapacity * 2 + 1;

Entry[] newMap = new Entry[newCapacity];

modCount++;

threshold = (int)(newCapacity * loadFactor);

table = newMap;

for (int i = oldCapacity ; i-- > 0 ;) {

for (Entry<K,V> old = oldMap[i] ; old != null ; ) {

Entry<K,V> e = old;

old = old.next;

int index = (e.hash & 0x7FFFFFFF) % newCapacity;

e.next = newMap[index];

newMap[index] = e;

}

}

}

 

void addEntry(int hash, K key, V value, int bucketIndex) {

Entry<K,V> e = table[bucketIndex];

table[bucketIndex] = new Entry<K,V>(hash, key, value, e);

if (size++ >= threshold)

resize(2 * table.length);

}

 

哈希值的使用

HashTable直接使用对象的hashCode,代码是这样的:

public synchronized booleancontainsKey(Object key) {

Entry tab[] = table;

int hash = key.hashCode();

int index = (hash & 0x7FFFFFFF) % tab.length;

for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {

if ((e.hash == hash) && e.key.equals(key)) {

return true;

}

}

return false;

}

HashMap重新计算hash值,而且用与代替求模

public boolean containsKey(Object key) {

Object k = maskNull(key);

int hash = hash(k.hashCode());

int i = indexFor(hash, table.length);

Entry e = table[i];

while (e != null) {

if (e.hash == hash && eq(k, e.key))

return true;

e = e.next;

}

return false;

}

 

原帖地址:http://java.chinaitlab.com/base/815735.html

posted @ 2013-04-24 02:41  ScottChiang  阅读(170)  评论(0)    收藏  举报