常用的方法可以先拿到当前map所有的key 根据key迭代出所有的value
Map.Entry是Map的一个工具类,提供了从map中读取一行数据的读取,一次可以迭代出map集合中的键值对,或者说一次迭代一行数据而不是一个key
[java] view plain copy
在CODE上查看代码片派生到我的代码片
Map集合取数据 方法汇总
// 创建集合 Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "周一"); map.put(2, "周二"); map.put(3, "周三");
[java] view plain copy
在CODE上查看代码片派生到我的代码片
// 方法一: for each + map.keySet();
Set<Integer> set = map.keySet();
for (Integer key : set) { System.out.println("key:" + key + ", value:" + (key));
}
[java] view plain copy
在CODE上查看代码片派生到我的代码片
// 方法二:while循环(Iterator + map.keySet();)
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while(it.hasNext()){ Integer key = it.next(); System.out.println("key:" + key + ", value:" + (key));
}
[java] view plain copy
在CODE上查看代码片派生到我的代码片
// 方法三:while循环(Iterator + Map.Entry<Key, Value>)
Iterator<Map.Entry<Integer, String>> it = Set().iterator();
while (it.hasNext()) { Map.Entry<Integer, String> entry = it.next(); System.out.println("key:"Key() + ", value:"Value());
}
[java] view plain copy
在CODE上查看代码片派生到我的代码片
// 方法四:for循环(Iterator + Map.Entry<Key, Value>)
for(Iterator<Map.Entry<Integer, String>> it = Set().iterator(); it.hasNext();){ Map.Entry<Integer, String> entry = it.next(); System.out.println("key:" + Key() + ", value:" Value());
}
[java] view plain copy
在CODE上查看代码片派生到我的代码片
// 方法五:for each + Map.Entry<Key, Value>
for(Map.Entry<Integer, String> entry : Set()){ System.out.println("key:" + Key() + ", value:" Value());
}
常用的方法:
HashMap类
Set keySet()
返回当前map所有的key的Set集合。
Set
本文发布于:2024-02-05 02:05:23,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170721577262033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |