hashmap的iterator会遍历entry单向链表吗

ottorz 2014-05-03 02:54:12
hashmap的iterator会遍历entry单向链表吗
...全文
911 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ottorz 2014-05-04
  • 打赏
  • 举报
回复
引用 5 楼 yankaiwuzhengbo 的回复:
但是使用map如果还使用keyset的话会认为你很没有水平。。。
我是在想这个entrySet的意义,为什么不直接Map.iterator
yuhouqingchen_2648 2014-05-04
  • 打赏
  • 举报
回复
但是使用map如果还使用keyset的话会认为你很没有水平。。。
lymoge 2014-05-03
  • 打赏
  • 举报
回复
得到Entry的集合?
ottorz 2014-05-03
  • 打赏
  • 举报
回复
引用 1 楼 huxiweng 的回复:
你说的是对key的iterator吧。hashmap本身是没有iterator的。 public Set<Map.Entry<K,V>> entrySet() http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#entrySet()
java为什么要entrySet呢?
ottorz 2014-05-03
  • 打赏
  • 举报
回复
看到源码了

private abstract class HashIterator<E> implements Iterator<E> {
        Entry<K,V> next;	// next entry to return
        int expectedModCount;	// For fast-fail
        int index;		// current slot
        Entry<K,V> current;	// current entry

        HashIterator() {
            expectedModCount = modCount;
            if (size > 0) { // advance to first entry
                Entry[] t = table;
                while (index < t.length && (next = t[index++]) == null)   //把数组下标移到第一个不为空的
                    ;
            }
        }

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

        final Entry<K,V> nextEntry() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            Entry<K,V> e = next;
            if (e == null)
                throw new NoSuchElementException();

            if ((next = e.next) == null) {                                              //  检查当前entry链表中是否还有next,没有则移动到下一条entry链
                Entry[] t = table;
                while (index < t.length && (next = t[index++]) == null)   
                    ;
            }
	    current = e;
            return e;
        }

        public void remove() {
            if (current == null)
                throw new IllegalStateException();
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            Object k = current.key;
            current = null;
            HashMap.this.removeEntryForKey(k);
            expectedModCount = modCount;
        }

    }
teemai 2014-05-03
  • 打赏
  • 举报
回复
你说的是对key的iterator吧。hashmap本身是没有iterator的。 public Set<Map.Entry<K,V>> entrySet() http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#entrySet()

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧