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() );
}
HashMap遍历
最新推荐文章于 2021-10-12 10:21:03 发布