ArrayList、LinkedList、HashMap分析
1. ArrayList 分析
ArrayList < Integer > list = new ArrayList < > ( ) ;
list. add ( 1 ) ;
list. add ( 2 ) ;
private static final int DEFAULT_CAPACITY = 10 ;
private static final Object [ ] EMPTY_ELEMENTDATA = { } ;
transient Object [ ] elementData;
private int size;
public ArrayList ( ) {
super ( ) ;
this . elementData = EMPTY_ELEMENTDATA;
}
public boolean add ( E e) {
ensureCapacityInternal ( size + 1 ) ;
elementData[ size++ ] = e;
return true ;
}
private void ensureCapacityInternal ( int minCapacity) {
if ( elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math . max ( DEFAULT_CAPACITY, minCapacity) ;
}
ensureExplicitCapacity ( minCapacity) ;
}
private void ensureExplicitCapacity ( int minCapacity) {
modCount++ ;
if ( minCapacity - elementData. length > 0 )
grow ( minCapacity) ;
}
private void grow ( int minCapacity) {
int oldCapacity = elementData. length;
int newCapacity = oldCapacity + ( oldCapacity >> 1 ) ;
if ( newCapacity - minCapacity < 0 )
newCapacity = minCapacity;
if ( newCapacity - MAX_ARRAY_SIZE > 0 )
newCapacity = hugeCapacity ( minCapacity) ;
elementData = Arrays . copyOf ( elementData, newCapacity) ;
}
2. LinkedList 分析
LinkedList < Integer > list = new LinkedList < > ( ) ;
list. add ( 1 ) ;
list. add ( 2 ) ;
transient int size = 0 ;
transient Node < E > first;
transient Node < E > last;
public LinkedList ( ) {
}
public boolean add ( E e) {
linkLast ( e) ;
return true ;
}
void linkLast ( E e) {
final Node < E > l = last;
final Node < E > newNode = new Node < > ( l, e, null ) ;
last = newNode;
if ( l == null )
first = newNode;
else
l. next = newNode;
size++ ;
modCount++ ;
}
private static class Node < E > {
E item;
Node < E > next;
Node < E > prev;
Node ( Node < E > prev, E element, Node < E > next) {
this . item = element;
this . next = next;
this . prev = prev;
}
}
3. ArrayList和LinkedList的区别
线程是否安全:它们两个都是线程不安全的,因为方法都没有用到synchronized 底层数据结构:ArrayList使用数组,LinkedList使用双向循环链表 插入删除是否受位置的影响:ArrayList因为使用的是数组,所有插入的删除的时间复杂度为O(n),而LinkedList的为O(1) 是否支持快速随机访问 :ArrayList支持随机访问,LinkedList不支持。
4. HashSet 和 TreeSet的比较
基于HashCode计算元素存放的位置 当存入元素的hashCode值相同时,会调用equals方法进行确认,如结果为true,则拒绝后者存入。
基于排列顺序实现元素的不重复 实现了SortedSet接口,对集合元素自动排序 元素对象的类型必须实现Comparable接口,执行排序规则 通过CompareTo方法确定是否为重复元素
4. HashMap 分析
HashMap < String , String > map = new HashMap < > ( ) ;
map. put ( "1" , "11" ) ;
map. put ( "2" , "22" ) ;
Set < String > keySet = map. keySet ( ) ;
Set < Map. Entry < String , String > > entrySet = map. entrySet ( ) ;
for ( Map. Entry < String , String > entry : entrySet) {
System . out. println ( entry. getKey ( ) + " , " + entry. getValue ( ) ) ;
}
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4 ;
static final int MAXIMUM_CAPACITY = 1 << 30 ;
static final float DEFAULT_LOAD_FACTOR = 0.75f ;
static final int TREEIFY_THRESHOLD = 8 ;
static final int UNTREEIFY_THRESHOLD = 6 ;
static final int MIN_TREEIFY_CAPACITY = 64 ;
transient Node < K , V > [ ] table;
transient int size;
public HashMap ( ) {
this . loadFactor = DEFAULT_LOAD_FACTOR;
}
public V put ( K key, V value) {
return putVal ( hash ( key) , key, value, false , true ) ;
}
final V putVal ( int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node < K , V > [ ] tab; Node < K , V > p; int n, i;
if ( ( tab = table) == null || ( n = tab. length) == 0 )
n = ( tab = resize ( ) ) . length;
if ( ( p = tab[ i = ( n - 1 ) & hash] ) == null )
tab[ i] = newNode ( hash, key, value, null ) ;
else {
. . . . . .
}
}