package map;
import java.util.*;
public class Map01 {
public static void main(String[] args) {
Map map = new HashMap();
map.put("贵州","贵阳");
map.put("辽宁","沈阳");
map.put("浙江","杭州");
map.put(new String("浙江"),"杭州");
/*Map map1 = new HashMap();
map1.putAll(map);
System.out.println(map1);
System.out.println(map.get("贵州"));
//map.clear();
System.out.println(map.containsKey("贵州"));
System.out.println(map.containsValue("贵阳"));*/
//Hashmap遍历
//1.通过EntrySet
System.out.println("第一种方式EntrySet的迭代器:");
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
//EntrySet的for增强
System.out.println("EntrySet的for增强:");
Set entrySet = map.entrySet();
for (Object obj: entrySet){
System.out.println(obj);
}
//2.先取出所有key, 通过key 取出对应的value
Set keyset = map.keySet();
System.out.println("第二种方式:");
for (Object key: keyset) {
System.out.println(key);
}
//3.直接使用values值
System.out.println("第三种的for增强:");
Collection valueset = map.values();
for( Object obj: valueset) {
System.out.println(obj);
}
System.out.println("第三种的迭代器:");
Iterator iterator1 = valueset.iterator();
while (iterator1.hasNext()) {
Object next = iterator1.next();
System.out.println(next);
}
}
}
Map常用的遍历方法
于 2023-04-29 15:49:20 首次发布