Map读取键值对,Java遍历Map的两种实现方法

2018-03-14 18:45:57

第一种方法是根据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);
  }
 }
}

  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 2017-11-19 00:17:51

    Java如何获取Date的“昨天”与“明天”示例代码

    最近在做项目的时候用到Date和Calendar比较多,而且用到的方式也比较全,突然想到一个问题,Java如何获取Date的"昨天"与"明天",也就是前一天和后一天呢?思考后写出了方法,想着万一以后用到,就总结出来,也方便有需要的朋友们参考借鉴,下面来一起看看吧。

  • 2017-11-23 02:00:51

    js 分页插件twbs-pagination

    ​cdn地址 http://www.bootcdn.cn/twbs-pagination/ 官网地址 可以在cdn地址上面查看到