这个是HashMap的内部类,通过EntrySet用来存储所有的map的值。为什么我们用Iterator可以来进行遍历map的数据?原因就是有这个内部类,EntrySet ---> AbstractSet --> Set --> Collection --> Iterator; (--> 表示继承或是实现关系)
这样们就可以通过Iterator来迭代出HashMap的值了
// 内部内,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();
}
}
}
迭代测试代码:
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());
}