遍历Map

来源:互联网 发布:淘宝手机搜索不精准 编辑:程序博客网 时间:2024/04/30 12:32

Map提供了一些常用方法,如keySet()、entrySet()等方法。
定义一个Map:

 Map<String, Object> map = new HashMap<>();        map.put("Page", 1);        map.put("Row", 10);        map.put("IsUser", 0);        map.put("Type", 1);        map.put("UserId", 0);

keySet()方法返回值是Map中key值的集合;

  map.keySet().toString()返回如下: [IsUser, UserId, Type, Page, Row]

entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry。

map.entrySet().toString()返回如下:[IsUser=0, UserId=0, Type=1, Page=1, Row=10]

Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry

  for (Map.Entry entry : map.entrySet()) {            Log.d("xxmap", entry.getKey() + ";" + entry.getValue().toString());        }

返回值为:

 D/xxmap:IsUser;0 D/xxmap: UserId;0 D/xxmap: Type;1 D/xxmap: Page;1 D/xxmap: Row;10
0 0
原创粉丝点击