public class TestHashMap {
@Test
public void test01() {
HashMap<Object, Object> map = new HashMap<>();
// 传入成绩
map.put("数学", 100);
map.put("英语", 100);
map.put("语文", 100);
// 根据key来获取value
System.out.println(map.get("数学"));
// 查询key
System.out.println(map.containsKey("语文"));
// 查询值
System.out.println(map.containsValue(10));
// 遍历map里面的key和value
for (Object key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
// java迭代器输出hashmap
// 创建一个迭代器
Iterator<Map.Entry<Object, Object>> iterator = map.entrySet().iterator();
// 测试entrySet的作用 获取hashmap里面的key和value
System.out.println(map.entrySet());
// 获取hashmap里面的key
System.out.println(map.keySet());
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
HashMap遍历的方式和迭代器的学习
最新推荐文章于 2024-01-12 05:15:00 发布