Java遍历Map的两种实现方法

第一种方法是根据map的keyset()方法来获取key的set集合,然后遍历map取得value的值
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class HashMapTest2
{
public static void main(String[] args)
{
  HashMap map = new HashMap();
 
  map.put("a","aaaa");
  map.put("b","bbbb");
  map.put("c","cccc");
  map.put("d","dddd");
 
  Set set = map.keySet();
 
  for(Iterator iter = set.iterator(); iter.hasNext();)
  {
   String key = (String)iter.next();
   String value = (String)map.get(key);
   System.out.println(key+"===="+value);
  }
}
}

第二种方式是使用Map.Entry来获取:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapTest4
{
public static void main(String[] args)
{
  HashMap map = new HashMap();
 
  map.put("a","aa");
  map.put("b","bb");
  map.put("c","cc");
  map.put("d","dd");
 
  Set set = map.entrySet();
 
  for(Iterator iter = set.iterator(); iter.hasNext();)
  {
   Map.Entry entry = (Map.Entry)iter.next();
  
   String key = (String)entry.getKey();
   String value = (String)entry.getValue();
   System.out.println(key +" :" + value);
  }
}
}

 

你可能感兴趣的:(java)