java集合框架类源代码阅读体会(Java Collections Framework)
忘了什么原因突然想看下JCF,于是就有了这个阅读体会。
java版本基于sun jdk1.6.0_18
1 通用接口
public interface Iterable<T>
public interface Iterator<E>
一个典型的iterator模式的应用。
注意注释中提到的Iterator和enumerations一个不同点是方法名的提高,命名还是很重要的。
public interface Collection<E>
extends Iterable<E>
比较有意思。
线程策略由实现类决定。
注意contains并不是一定要使用equals,而是把自由给了实现类。
很多可选操作。
如果要继承equals方法需要特别小心,默认的约定是List和Set永远不相等。
// Query Operationsint size();boolean isEmpty();boolean contains(Object o);Iterator<E> iterator();Object[] toArray();<T> T[] toArray(T[] a);// Modification Operationsboolean add(E e);boolean remove(Object o);// Bulk Operationsboolean containsAll(Collection<?> c);boolean addAll(Collection<? extends E> c);boolean removeAll(Collection<?> c);boolean retainAll(Collection<?> c);void clear();// Comparison and hashingboolean equals(Object o);int hashCode();
public boolean removeAll(Collection<?> c) { boolean modified = false; if (size() > c.size()) { for (Iterator<?> i = c.iterator(); i.hasNext(); ) modified |= remove(i.next()); } else { for (Iterator<?> i = iterator(); i.hasNext(); ) { if (c.contains(i.next())) { i.remove(); modified = true; } } } return modified; }
// Positional Access OperationsE get(int index);E set(int index, E element);void add(int index, E element);E remove(int index);// Search Operationsint indexOf(Object o);int lastIndexOf(Object o);// List IteratorsListIterator<E> listIterator();ListIterator<E> listIterator(int index);// ViewList<E> subList(int fromIndex, int toIndex);

public V get(Object key) {Iterator<Entry<K,V>> i = entrySet().iterator();if (key==null) { while (i.hasNext()) {Entry<K,V> e = i.next();if (e.getKey()==null) return e.getValue(); }} else { while (i.hasNext()) {Entry<K,V> e = i.next();if (key.equals(e.getKey())) return e.getValue(); }}return null; }


final Entry<K,V> getEntry(Object key) { // Offload comparator-based version for sake of performance if (comparator != null) return getEntryUsingComparator(key); if (key == null) throw new NullPointerException();Comparable<? super K> k = (Comparable<? super K>) key; Entry<K,V> p = root; while (p != null) { int cmp = k.compareTo(p.key); if (cmp < 0) p = p.left; else if (cmp > 0) p = p.right; else return p; } return null; } final Entry<K,V> getEntryUsingComparator(Object key) {K k = (K) key; Comparator<? super K> cpr = comparator; if (cpr != null) { Entry<K,V> p = root; while (p != null) { int cmp = cpr.compare(k, p.key); if (cmp < 0) p = p.left; else if (cmp > 0) p = p.right; else return p; } } return null; } final Entry<K,V> getCeilingEntry(K key) { Entry<K,V> p = root; while (p != null) { int cmp = compare(key, p.key);//进入到左子树,说明该子树的root比key大。 if (cmp < 0) { if (p.left != null) p = p.left; else return p; } else if (cmp > 0) { if (p.right != null) { p = p.right; } else {//如果是左子树进来的,查找该左子树的root。如果不是,结果是null。 Entry<K,V> parent = p.parent; Entry<K,V> ch = p; while (parent != null && ch == parent.right) { ch = parent; parent = parent.parent; } return parent; } } else return p; } return null; }@Test(expected = NullPointerException.class)public void testAddNullToTreeMap() {TreeMap<String, String> tm = new TreeMap<String, String>();tm.put(null, "test");tm.get("key");}// deleted entries are replaced by their successors if (lastReturned.left != null && lastReturned.right != null) next = lastReturned;