程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 淺談JAVA中HashMap、ArrayList、StringBuilder等的擴容機制,hashmaparraylist

淺談JAVA中HashMap、ArrayList、StringBuilder等的擴容機制,hashmaparraylist

編輯:JAVA綜合教程

淺談JAVA中HashMap、ArrayList、StringBuilder等的擴容機制,hashmaparraylist


JAVA中的部分需要擴容的內容總結如下:
第一部分:

HashMap<String, String> hmap=new HashMap<>();
HashSet<String> hset=new HashSet<>();
Hashtable<String, String> htable=new Hashtable<>();
第二部分:

CopyOnWriteArrayList<String> coarray=new CopyOnWriteArrayList<>();
ArrayList<String> array=new ArrayList<>();
Vector<String> vec=new Vector<>();
第三部分:

StringBuffer sb=new StringBuffer();
StringBuilder sbu=new StringBuilder();
先從以下幾個源碼方面分析:(JDK1.8)

1、初始容量。
2、擴容機制。
3、同類型之間對比。

1.1 HashMap:
一、初始容量定義:默認為1 << 4(16)。最大容量為1<< 30。
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
二、擴容加載因子為(0.75),第一個臨界點在當HashMap中元素的數量等於table數組長度*加載因子(16*0.75=12),
如果超出則按oldThr << 1(原長度*2)擴容。
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold

1.2 HashSet

一、初始容量定義:16。因為構造一個HashSet,其實相當於新建一個HashMap,然後取HashMap的Key。
擴容機制和HashMap一樣。
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}

1.3 Hashtable<String, String> htable=new Hashtable<>();
public class Hashtable<K,V>
extends Dictionary<K,V>
一、初始容量定義:capacity (11)。
/**
* Constructs a new, empty hashtable with a default initial capacity (11)
* and load factor (0.75).
*/
public Hashtable() {
this(11, 0.75f);
}
二、擴容加載因子(0.75),當超出默認長度(int)(11*0.75)=8時,擴容為old*2+1。
int newCapacity = (oldCapacity << 1) + 1;

小結:HashTable和HashMap區別

第一,繼承不同。
public class Hashtable extends Dictionary implements Map
public class HashMap extends AbstractMap implements Map
第二:
Hashtable 中的方法是同步的,而HashMap中的方法在缺省情況下是非同步的。在多線程並發的環境下,可以直接使用
Hashtable,但是要使用HashMap的話就要自己增加同步處理了。
第三,Hashtable中,key和value都不允許出現null值。

在HashMap中,null可以作為鍵,這樣的鍵只有一個;可以有一個或多個鍵所對應的值為null。當get()方法返回null值時,
即可以表示 HashMap中沒有該鍵,也可以表示該鍵所對應的值為null。因此,在HashMap中不能由get()方法來判斷HashMap中
是否存在某個鍵, 而應該用containsKey()方法來判斷。

第四,兩個遍歷方式的內部實現上不同。

Hashtable、HashMap都使用了 Iterator。而由於歷史原因,Hashtable還使用了Enumeration的方式 。

第五,哈希值的使用不同,HashTable直接使用對象的hashCode。而HashMap重新計算hash值。

第六,
Hashtable和HashMap它們兩個內部實現方式的數組的初始大小和擴容的方式。HashTable中hash數組默認大小是11,增加的方
式是 old*2+1。HashMap中hash數組的默認大小是16, 增加的方式是 old*2。


2.1 CopyOnWriteArrayList:
/**
* Creates an empty list.
*/
public CopyOnWriteArrayList() {
setArray(new Object[0]);
}
CopyOnWriteArrayList在做修改操作時,每次都是重新創建一個新的數組,在新數組上操作,最終再將新數組替換掉原數組
。因此,在做修改操作時,仍可以做讀取操作,讀取直接操作的原數組。讀和寫操作的對象都不同,因此讀操作和寫操作互
不干擾。只有寫與寫之間需要進行同步等待。另外,原數組被聲明為volatile,這就保證了,一旦數組發生變化,則結果對
其它線程(讀線程和其它寫線程)是可見的。

CopyOnWriteArrayList並不像ArrayList一樣指定默認的初始容量。它也沒有自動擴容的機制,而是添加幾個元素,長度就相
應的增長多少。

CopyOnWriteArrayList適用於讀多寫少,既然是寫的情況少,則不需要頻繁擴容。並且修改操作每次在生成新的數組時就指
定了新的容量,也就相當於擴容了,所以不需要額外的機制來實現擴容。
2.2 ArrayList<String> array=new ArrayList<>();
一、初始容量定義:10。
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;

二、擴容:oldCapacity + (oldCapacity >> 1),即原集合長度的1.5倍。
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
2.3 Vector<String> vec=new Vector<>();
一、初始容量定義:10。
public Vector() {
this(10);
}
二、擴容:當擴容因子大於0時,新數組長度為原數組長度+擴容因子,否則新數組長度為原數組長度的2倍。
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
小結:
1,ArrayList與Vector初始容量都為10。
2,擴容機制不同,當超出當前長度時ArrayList擴展為原來的1.5倍,而若不考慮擴容因子Vector擴展為原來的2倍。

3,ArrayList為非線程安全的,處理效率上較Vector快,若同時考慮線程安全和效率,可以使用 CopyOnWriteArrayList。

3.1 StringBuffer sb=new StringBuffer();
一、初始容量定義:16。
public StringBuffer() {
super(16);
}
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
二、擴容:因為StringBuffer extends AbstractStringBuilder,所以實際上是用的是AbstractStringBuilder
的擴容方法,當用append(str),添加字符串時,假設字符串中已有字符長度為count的字符串,初始長度value=16,若要添加的
字符串長度(count+str.length())<=(value*2+2)則按value*2+2長度擴容,並且
value=value*2+2,若(count+str.length())>(value*2+2),則按count+str.length()長度擴容,並且
value=count+str.length()。下次超出時再按以上方法與value*2+2比較擴容。


private int newCapacity(int minCapacity) {
// overflow-conscious code
int newCapacity = (value.length << 1) + 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;

3.2 StringBuilder sbu=new StringBuilder();
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
public StringBuilder() {
super(16);
}
private int newCapacity(int minCapacity) {
// overflow-conscious code
int newCapacity = (value.length << 1) + 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
小結:
1.StringBuilder是jdk1.5引進的,而StringBuffer在1.0就有了;
2.StringBuilder和StringBuffer都是可變的字符串。能夠通過append或者insert等方法改動串的內容;
3.StringBuffer是線程安全的而StringBuilder不是,因而在多線程的環境下優先使用StringBuffer,而其它情況下推薦使用
StringBuilder,由於它更快。
4.StringBuilder和StringBuffer都繼承自AbstractStringBuilder類,AbStractStringBuilder主要實現了擴容、append、
insert方法。StrngBuilder和StringBuffer的相關方法都直接調用的父類。
5.StringBuilder和StringBuffer的初始容量都是16,程序猿盡量手動設置初始值。以避免多次擴容所帶來的性能問題;
6.StringBuilder和StringBuffer的擴容機制是這種:首先試著將當前數組容量擴充為原數組容量的2倍加上2,假設這個新容
量仍然小於預定的最小值(minimumCapacity),那麼就將新容量定為(minimumCapacity),最後推斷是否溢出,若溢出,
則將容量定為整型的最大值0x7fffffff。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved