Map的entrySet()方法

当调用hashmap的entrySet()时,得到是一个EntrySet内部类的对象。

Set<Map.Entry<K,V>>  entryset;
EntrySet时AbstractSet类的子类,实现了set的接口,所以能够引用给entryset
    public Set<Map.Entry<K,V>> entrySet() {
        Set<Map.Entry<K,V>> es;
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
    }

当我们调用上面得到的对象的方法时,如staff.entrySet().iterator().next(),此时返回外部类的一个对象,这个类继承了HashIterator,此时调用的是这个迭代器中的next,此时执行HashIterator中的nextNode,nextNode返回table中的下一个。得到是一个EntrySet内部类的对象。

final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<Map.Entry<K,V>> iterator() {
            return new EntryIterator();
        }
        public final boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
            Object key = e.getKey();
            Node<K,V> candidate = getNode(hash(key), key);
            return candidate != null && candidate.equals(e);
        }
        public final boolean remove(Object o) {
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
                Object key = e.getKey();
                Object value = e.getValue();
                return removeNode(hash(key), key, value, true, true) != null;
            }
            return false;
        }
        public final Spliterator<Map.Entry<K,V>> spliterator() {
            return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
            Node<K,V>[] tab;
            if (action == null)
                throw new NullPointerException();
            if (size > 0 && (tab = table) != null) {
                int mc = modCount;
                for (int i = 0; i < tab.length; ++i) {
                    for (Node<K,V> e = tab[i]; e != null; e = e.next)
                        action.accept(e);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }
final class EntryIterator extends HashIterator
        implements Iterator<Map.Entry<K,V>> {
        public final Map.Entry<K,V> next() { return nextNode(); }
    }

  

      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);
      }
    }
     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;
        }
    }

  

时间: 2024-08-26 15:16:23

Map的entrySet()方法的相关文章

集合框架Map之entrySet方法的使用

Map的entrySet函数的使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口,类似与我们熟悉的内部类一样,内部类定义在外部类内部,可以直接访问到外部类中的成员 package cn.itcast.map; import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set; public class Map

Map以及Set的遍历(EntrySet方法,补充enumeration和Iterator的区别)

public void mearge(Map map) { Map returnMap = new HashMap<>(); // 转换为Entry Set<Map.Entry<Object, Object>> entries = map.entrySet(); // 遍历 for (Map.Entry<Object, Object> entry : entries) { Object key = entry.getKey(); Object val = e

Java集合Set、List、Map的遍历方法

本文实例讲述了Java集合Set.List.Map的遍历方法,分享给大家供大家参考. 具体方法如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 7

Map的遍历方法

Part I:keySet() 和 get() 结合 1.Map对象通过使用keySet()方法获取所有的key值,返回一个Set对象.2.Map对象通过使用get()方法借助于key的值来遍历所有元素. Part II:Map.Entry 和 Map.entrySet() 结合 Map.entrySet() 方法返回的是一个Set<Map.Entry<K,V>>对象集合,集合中的对象是 Map.Entry. Map.Entry 是一个接口,表示一个映射项(里面有Key和Value

遍历Map集合四中方法

<embed wmode="transparent" src="http://chabudai.sakura.ne.jp/blogparts/honehoneclock/honehone_clock_tr.swf" quality="high" bgcolor="#ffffff" width="160" height="70" name="honehoneclock&qu

app framework map及ajax方法

$(function () { $.ajax({ url: 'Ashx/GetProductList.ashx', contentType: "JSON", success: function (data) { alert(data); var jsonData = eval("(" + data + ")"); alert(jsonData); $.map(jsonData, function (k, v) { alert("ssss

js数组的map与forEach方法的区别及兼容性用法

高级浏览器(包括ie9以上)支持map和forEach方法对数组循环遍历,用法基本相同,但有些区别必须知道,才能在项目中正确选择 原理: 高级浏览器支持forEach方法语法:forEach和map都支持2个参数:一个是回调函数(item,index,list)和上下文: forEach:用来遍历数组中的每一项:这个方法执行是没有返回值的,对原来数组也没有影响: 数组中有几项,那么传递进去的匿名回调函数就需要执行几次: 每一次执行匿名函数的时候,还给其传递了三个参数值:数组中的当前项item,当

在JavaScript函数式编程里使用Map和Reduce方法

所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工具方法我们现在可以使用了.在这些函数方法里主要的是基于JavaScript 数组对象的map()方法和reduce()方法. 如果你如今还没有使用map()和reduce()方法,那么现在是时候开始使用了.如今绝大部分的JavaScript开发平台都与生俱来的支持ECMAScript5.使用Map方

forEach和map和for方法的区别

那么接下来,我继续做分析,为什么更推荐用.map(),而不是.forEach()? 首先,.map()要比.forEach()执行速度更快.虽然我也说过执行速度不是我们需要考虑的主要因素,但是他们都比for()要更好用,那肯定要选更优化的一个. 第二,.forEach()的返回值并不是array.如果你想用函数式编程写个链式表达式来装个逼,.map()将会是你不二的选择. 来看下面这个例子: var arr = [1, 2, 3]; console.log( arr.map(function(i