Java 迭代器模式

迭代器模式:提供一种方法访问一个集合对象中的元素,而不需要知道集合对象的底层表示。即使集合底层发生了变化,只要集合的iterator()方法能返回的一个正确的Iterator对象即可。这样可以把集合和调用解耦。

在Java中,可以通过迭代器来遍历ArrayList。(通常还是直接for循环了)。

 ArrayList<String> list = new ArrayList<>();
 Iterator<String> iterator = list.iterator();
 while (iterator.hasNext()) {
       System.out.println(iterator.next());
 }

在Java中,可以通过迭代器来遍历HashMap。

使用Key来进行遍历

HashMap<String, String> hashMap = new HashMap<>();
Iterator<String> iterator1 = hashMap.keySet().iterator();
while (iterator1.hasNext()) {
    String key = iterator1.next();
    System.out.println("key:" + key + ",value:" + hashMap.get(key));
}

使用Map.Entry进行办理

Iterator<Map.Entry<String, String>> entryIterator = hashMap.entrySet().iterator();
while (entryIterator.hasNext()) {
    Map.Entry<String, String> entry = entryIterator.next();
    System.out.println(entry.getKey() + " , " + entry.getValue());
}

注意:这种方式可以使用for循环等价替代。

for (Map.Entry<String, String> entry : hashMap.entrySet()) {
    System.out.println(entry.getKey() + " , " + entry.getValue());
}

在Java中,集合都继承自Collection,而Collection继承了Iterable接口,在Iterable接口中定义了iterator()方法。

public interface Iterable<T> {
    Iterator<T> iterator();
}

iterator()方法返回的是一个Iterator对象。

public interface Iterator<E> {
   
    boolean hasNext();

    E next();

    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    //...省略其他方法
}

而不同的具体集合对Iterator<E>接口拥有不同的实现。

ArrayList的实现。

public Iterator<E> iterator() {
    return new Itr();
}
private class Itr implements Iterator<E> {
    int cursor;       // index of next element to return
    int lastRet = -1; // index of last element returned; -1 if no such
    int expectedModCount = modCount;

    public boolean hasNext() {
        return cursor != size;
    }

    @SuppressWarnings("unchecked")
    public E next() {
        checkForComodification();
        int i = cursor;
        if (i >= size)
            throw new NoSuchElementException();
        Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length)
            throw new ConcurrentModificationException();
        cursor = i + 1;
        return (E) elementData[lastRet = i];
    }

    public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();
        checkForComodification();

        try {
            ArrayList.this.remove(lastRet);
            cursor = lastRet;
            lastRet = -1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }

    final void checkForComodification() {
        if (modCount != expectedModCount)
           throw new ConcurrentModificationException();
    }
}

HashMap可以利用keySet进行遍历

public Set<K> keySet() {
    Set<K> ks = keySet;
    if (ks == null) {
        ks = new KeySet();
        keySet = ks;
    }
    return ks;
}

//KetSet类
final class KeySet extends AbstractSet<K> {
   
    public final Iterator<K> iterator(){ 
		   return new KeyIterator();
    }
    //...

}
//KeyIterator 类
 final class KeyIterator extends HashIterator
    implements Iterator<K> {
    public final K next() { return nextNode().key; }
}

//HashIterator 类
abstract class HashIterator {
    Node<K,V> next;        // next entry to return
    Node<K,V> current;     // current entry
    int expectedModCount;  // for fast-fail
    int index;             // current slot

    HashIterator() {
        expectedModCount = modCount;
        Node<K,V>[] t = table;
        current = next = null;
        index = 0;
        if (t != null && size > 0) { // advance to first entry
            do {} while (index < t.length && (next = t[index++]) == null);
        }
    }

    public final boolean hasNext() {
        return next != null;
    }

    final Node<K,V> nextNode() {
        Node<K,V>[] t;
        Node<K,V> e = next;
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        if (e == null)
            throw new NoSuchElementException();
        if ((next = (current = e).next) == null && (t = table) != null) {
            do {} while (index < t.length && (next = t[index++]) == null);
        }
        return e;
    }

    public final void remove() {
        Node<K,V> p = current;
        if (p == null)
            throw new IllegalStateException();
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        current = null;
        K key = p.key;
        removeNode(hash(key), key, null, false, false);
        expectedModCount = modCount;
    }
}

上面的代码展示了,ArrayList和HashMap内部是如何实现各自具体的迭代器的,但是我们在使用迭代器的时候,完全不用管它们内部的细节是如何实现的。我们所需要知道的所有一切就是:可以通过迭代器遍历集合就可以了。

最后再来看看迭代器模式的定义,是不是清晰很多了呢?

迭代器模式:提供一种方法访问一个集合对象中的元素,而不需要知道集合对象的底层表示。即使集合底层发生了变化,只要集合的iterator()方法能返回的一个正确的Iterator对象即可。这样可以把集合和调用解耦。

参考链接:

  • 《Head First 设计模式》
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值