1. AbstractCollection.toString()
避免出现死循环的现象,如果元素就是Collection本身,打印(this Collection)
public String toString() {
Iterator<E> i = iterator();
if (!i.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = i.next();
sb.append(e == this ? "(this Collection)" : e);
if (!i.hasNext())
return sb.append(']').toString();
sb.append(", ");
}
}
2. AbstractList.equals()
比较两个list,必须满足:list.size相等,元素相等且顺序相同。
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
ListIterator<E> e1 = listIterator();
ListIterator e2 = ((List) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1 == null ? o2 == null : o1.equals(o2)))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
3. ArrayList
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
...
}
ArrayList基本等同于Vector,只是Vector是线程安全的,而ArrayList不是。
内部数组实现,所以如果需要插入大量数据,最好是先调用ensureCapacity()来确保内部数组足够大。这样可以避免ArrayList在插入过程中频繁的增大数组大小。
如果需要让Arraylist是线程安全的,最直接的方法就是用List list = Collections.synchronizedList(new ArrayList(...));
The iterators returned by this class's iterator and listIterat http:// or 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
. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
每次大小的擴充都是原來的50%+1
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
int newCapacity = (oldCapacity * 3) / 2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
引申閱讀: 對象拷貝
3. LinkedList
transient 关键字:标明某些字段不要被序列化
private Entry<E> entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
+ size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}
允许Null作为key或者value(问题就是对于有些方法,不能通过判断返回值是否为Null来作为安全的判断条件, 比如下面的方法。)
V get(Object key);
The HashMap class is roughly equivalent to <tt>Hashtable</tt>, except that it is unsynchronized and permits nulls.
UML Diagram