HashMap 遍历的两种方式及性能比较
发布日期:2021-05-15 10:07:27 浏览次数:14 分类:博客文章

本文共 3424 字,大约阅读时间需要 11 分钟。

HashMap ���Java���������������������������������������������HashMap ������������������������������������������������������������������HashMap ���������������������������������������������������������������������������������������

������HashMap ������

���������������������HashMap ������������������������������������HashMap ������������������������������������������������������Entry���Entry ������������������������������HashMap ������������Entry ������������������Hash()������������������������������Entry������������������������������������������������������������for���������������������������������������������������HashMap ������

1. ������EntrySet������

HashMap ������������������EntrySet������������������������������������HashMap ������������������Entry���������������������������������HashMap���

HashMap
map=new HashMap
();

������������������������EntrySet���

Set
> entrySet=map.entrySet();

������������������HashMap ������������Entry���������Entry ���������������������������������������������foreach ������������

Set
> entrySet=map.entrySet();for (Entry
entry : entrySet) { System.out.println(entry.getKey()+":"+entry.getValue()); }

���������������������������������

Iterator
> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry
entry = iterator.next(); System.out.println(entry.getKey()+":"+entry.getValue()); }
2.������KeySet ������

HashMap ������KeySet������������������Map������������������������set���������������������get������������������������������������������������������ ������KeySet ���������������������:

for (String key : map.keySet()) {			System.out.println(key+":"+map.get(key));		}

���������������������������������Hashmap ������������value���������������values ������������������������������������������value ���collection���

Collection
collection = map.values();System.out.println(collection);

������������������

���������������������������������������������������������������������������������������������������������������������������EntrySet ������������������������������������������������������������������������������foreach������������������������������������EntrySet ������������������������KeySet���������������������������������KeySet������������������������������������������������KeySet ������������get() ������������������JDK 1.6 ver������

public V get(Object key) {        if (key == null)            return getForNullKey();        int hash = hash(key.hashCode());        for (Entry
e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) return e.value; } return null; }

������������������������������������key ���������hash��� ���������������hash������HashMap ���Entry ������������������hash������key���hash������������Entry���������������������Value���������get() ���������������Value ������������������������������������������������������������������

������������

������������������������������������������HashMap ������������������������EntrySet������������������KeySet������������EntrySet ������KeySet ���������������KeySet ���get���������������Value ������������������������������������������������HashMap ���������������EntrySet ������������

上一篇:如何在命令提示符下编译运行含有Package的java文件
下一篇:简单方法实现无刷新提交Form表单

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月20日 14时06分57秒