HashMap.Entry source code

This is an internal class of HashMap, used to store all map values ​​through EntrySet. Why can we use Iterator to traverse the map data? The reason is that there is this internal class, EntrySet ---> AbstractSet --> Set --> Collection --> Iterator; (--> indicates inheritance or realization relationship)

In this way, we can iterate the value of HashMap through Iterator

    // 内部内,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();
        }
        // 判断是否包含传入 的key
        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();
            }
        }
    }

Iterative test code:

		Iterator<Entry<String, String>>  iterator = map.entrySet().iterator();
		
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			
			String key = entry.getKey();
			System.out.println(key);
			System.out.println(entry.getValue());
			System.out.println(entry.hashCode());
		}
		

 

Guess you like

Origin blog.csdn.net/qq_26896085/article/details/105090605