map集合特点及遍历的方法
map集合特点:无序、以键值对的形式添加元素,键不能重复,值可以重复
没有继承Collection接口。
如下有一map集合:
Map
map.put("张一", "男");
map.put("张二", "男");
map.put("张三", "男");
map.put("张四", "男");
//第一种通过加强for循环map.keySet(),然后通过键key获取到value值
for(String s:map.keySet()){
System.out.println("key : "+s+" value : "+map.get(s));
注:遍历值的方法为map.values();
//第二种通过Map.Entry
for(Map.Entry
System.out.println("键 key :"+entry.getKey()+" 值value :"+entry.getValue());
//第三种Iterator遍历获取,然后获取到Map.Entry
Iterator
while(it.hasNext()){
Map.Entry
System.out.println("键key :"+entry.getKey()+" value :"+entry.getValue());