Map虽然实现了Collection接口,但是并不能直接遍历。如果想遍历Map,需要使用entrySet
view plaincopy to clipboardprint?
/**
*Entry: java.util.Entry
*hashmap类型为HashMap
*/
for(Iterator iter=hashmap.entrySet().iterator(); iter.hasNext();){
Entry entry = (Entry)iter.next();
entry.getKey(); //返回与此项对应的键
entry.getValue(); //返回与此项对应的值
}
/**
*Entry: java.util.Entry
*hashmap类型为HashMap
*/
for(Iterator iter=hashmap.entrySet().iterator(); iter.hasNext();){
Entry entry = (Entry)iter.next();
entry.getKey(); //返回与此项对应的键
entry.getValue(); //返回与此项对应的值
}
另外一种方式是使用keySet进行遍历,看别人的文章和程序运行结果来看,似乎要慢一些。因为对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。
而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。
首选应该是上面的方法。
view plaincopy to clipboardprint?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Calendar;
public class HashMapTest {
public static void main(String[] args) {
HashMap hashmap = new HashMap();
for(int i=0;i<1000;i++){
hashmap.put(""+i,"hello");
}
long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
//String value = "";
while(iterator.hasNext()) {
//value = hashmap.get(iterator.next());
System.out.println(hashmap.get(iterator.next()));
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
listHashMap();
}
public static void listHashMap(){
java.util.HashMap hashmap = new java.util.HashMap();
for(int i=0;i<1000;i++){
hashmap.put(""+i,"hello");
}
long bs = Calendar.getInstance().getTimeInMillis();
//Set set = hashmap.entrySet() ;
java.util.Iterator it = hashmap.entrySet().iterator();
while(it.hasNext()){
java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.println(entry.getValue());
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Calendar;
public class HashMapTest {
public static void main(String[] args) {
HashMap hashmap = new HashMap();
for(int i=0;i<1000;i++){
hashmap.put(""+i,"hello");
}
long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
//String value = "";
while(iterator.hasNext()) {
//value = hashmap.get(iterator.next());
System.out.println(hashmap.get(iterator.next()));
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
listHashMap();
}
public static void listHashMap(){
java.util.HashMap hashmap = new java.util.HashMap();
for(int i=0;i<1000;i++){
hashmap.put(""+i,"hello");
}
long bs = Calendar.getInstance().getTimeInMillis();
//Set set = hashmap.entrySet() ;
java.util.Iterator it = hashmap.entrySet().iterator();
while(it.hasNext()){
java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.println(entry.getValue());
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
}
}
HashSet hs;
Iterator it = hs.iterator();
Object obj = null;
while(it.hasNext())
{
obj = it.next();
}
二.遍历HashSet
Set set = new HashSet();
for(int i=0;i<100;i++)
{
set .add("123");
}
for(Iterator it=set.iterator();it.hasNext();)
{
System.out.println(it.next());
}
三.遍历Hashtable(同步、线程安全的)
Hashtable table = new Hashtable();
table.put(1, "1");
table.put(2, "1");
table.put(3, "1");
//遍历key
Enumeration e = table.keys();
while( e. hasMoreElements() ){
System.out.println( e.nextElement() );
}
//遍历value
e = table.elements();
while( e. hasMoreElements() ){
System.out.println( e.nextElement() );
}
分享到:
相关推荐
Java 实例 - HashMap遍历源代码-详细教程.zip
自己写的例子,关于HashSet遍历和HashMap遍历的. 感谢大家参考
下面小编就为大家带来一篇基于HashMap遍历和使用方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Java HashMap 如何正确遍历并删除元素的方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
哈希树遍历 HashMap遍历和使用 HashMap遍历和使用
在项目用到了HashMap,需要遍历,并取到需要的值,具体实现代码如下,感兴趣的朋友可以参考下哈
在本篇文章中小编给大家分享了关于HashMap和List遍历方法及如何遍历删除元素知识点总结,需要的朋友们参考下。
下面小编就为大家带来一篇使用多种方式实现遍历HashMap的方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Java5种遍历HashMap数据的写法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Java 集合框架+实例 框架介绍了集合接口、集合类、集合算法等概念 实例包括集合比较、HashMap遍历、集合长度、集合遍历、集合输出、List 循环移动元素、遍历 HashTable 的键值等案例
HashMap是一种基于哈希表的Map接口实现,主要用于存储键值对。它允许空值和空键。其主要特点是通过键的哈希值存储值,并提供了添加、获取和操作存储值的方法。 HashMap的底层数据结构是由数组和链表组成的。数组是...
hashMap和hashTable的区别,大家可以下载学习学习。
主要介绍了java遍历HashMap简单的方法,以实例形式简单分析了采用java遍历HashMap的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
可以通过2种方法遍历HashMap <br>Map map = new HashMap(); <br>for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { <br> Map.Entry entry = (Map.Entry) iter.next(); <br> Object ...
看完这篇 HashMap,和面试官扯皮就没问题了 - HashMap 概述 - HashMap 和 HashTable 的区别 - 相同点 - 不同点 - HashMap 和 HashSet 的... - HashMap 的遍历方式 - HashMap 中的移除方法 - 关于 HashMap 的面
HashMap LinkedList 遍历方法 传统遍历方法 for(int i=0;i<list.size();i++) { String str = list.get(i); ... } 内置迭代器 for (String str : list) { ... } 显式迭代器 Iterator it = list.iterator(); ...
Map的遍历大体有3种: 1、遍历Map.entrySet():它的每一个元素都是Map.Entry对象,这个对象中, 放着的就是Map中的某一对key-value; 2、遍历Map.keySet():它是Map中key值的集合,我们可以通过遍历这个集合来 ...
首先用HashMap遍历一遍数组nums1,Key值储存数组元素,Value(初始值为1)值储存重复元素出现次数,每出现一次加1; b.用List储存nums2中与nums1开始intersection的元素,当遇到map中包含的Key时,才判断对应Value...
主要介绍了Java HashMap三种循环遍历方式及其性能对比,结合具体实例形式分析了Java HashMap三种循环遍历方式的实现方法、运行效率及性能优劣,需要的朋友可以参考下