转自:http://www.fengfly.com/plus/view-213898-7.html
返回所有的value
values()的实现代码如下:
- public Collection<V> values() {
- Collection<V> vs = values;
- return (vs != null) ? vs : (values = new Values());
- }
说明:从中,我们可以发现values()是通过 new Values() 来实现 “返回TreeMap中值的集合”。
那么Values()是如何实现的呢? 没错!由于返回的是值的集合,那么Values()肯定返回一个集合;而Values()正好是集合类Value的构造函数。Values继承于AbstractCollection,它的代码如下:
- // ”TreeMap的值的集合“对应的类,它集成于AbstractCollection
- class Values extends AbstractCollection<V> {
- // 返回迭代器
- public Iterator<V> iterator() {
- return new ValueIterator(getFirstEntry());
- }
- // 返回个数
- public int size() {
- return TreeMap.this.size();
- }
- // "TreeMap的值的集合"中是否包含"对象o"
- public boolean contains(Object o) {
- return TreeMap.this.containsValue(o);
- }
- // 删除"TreeMap的值的集合"中的"对象o"
- public boolean remove(Object o) {
- for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {
- if (valEquals(e.getValue(), o)) {
- deleteEntry(e);
- return true;
- }
- }
- return false;
- }
- // 清空删除"TreeMap的值的集合"
- public void clear() {
- TreeMap.this.clear();
- }
- }
说明:从中,我们可以知道Values类就是一个集合。而 AbstractCollection 实现了除 size() 和 iterator() 之外的其它函数,因此只需要在Values类中实现这两个函数即可。
size() 的实现非常简单,Values集合中元素的个数=该TreeMap的元素个数。(TreeMap每一个元素都有一个值嘛!)
iterator() 则返回一个迭代器,用于遍历Values。下面,我们一起可以看看iterator()的实现:
- public Iterator<V> iterator() {
- return new ValueIterator(getFirstEntry());
- }
说明: iterator() 是通过ValueIterator() 返回迭代器的,ValueIterator是一个类。代码如下:
- final class ValueIterator extends PrivateEntryIterator<V> {
- ValueIterator(Entry<K,V> first) {
- super(first);
- }
- public V next() {
- return nextEntry().value;
- }
- }
说明:ValueIterator的代码很简单,它的主要实现应该在它的父类PrivateEntryIterator中。下面我们一起看看PrivateEntryIterator的代码:
- abstract class PrivateEntryIterator<T> implements Iterator<T> {
- // 下一节点
- Entry<K,V> next;
- // 上一次返回的节点
- Entry<K,V> lastReturned;
- // 修改次数统计数
- int expectedModCount;
- PrivateEntryIterator(Entry<K,V> first) {
- expectedModCount = modCount;
- lastReturned = null;
- next = first;
- }
- // 是否存在下一个节点
- public final boolean hasNext() {
- return next != null;
- }
- // 返回下一个节点
- final Entry<K,V> nextEntry() {
- Entry<K,V> e = next;
- if (e == null)
- throw new NoSuchElementException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- next = successor(e);
- lastReturned = e;
- return e;
- }
- // 返回上一节点
- final Entry<K,V> prevEntry() {
- Entry<K,V> e = next;
- if (e == null)
- throw new NoSuchElementException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- next = predecessor(e);
- lastReturned = e;
- return e;
- }
- // 删除当前节点
- public void remove() {
- if (lastReturned == null)
- throw new IllegalStateException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- // deleted entries are replaced by their successors
- if (lastReturned.left != null && lastReturned.right != null)
- next = lastReturned;
- deleteEntry(lastReturned);
- expectedModCount = modCount;
- lastReturned = null;
- }
- }
说明:PrivateEntryIterator是一个抽象类,它的实现很简单,只只实现了Iterator的remove()和hasNext()接口,没有实现next()接口。
而我们在ValueIterator中已经实现的next()接口。
至此,我们就了解了iterator()的完整实现了。