HashMap遍历


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

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值