170530、java 迭代hashmap常用的三种方法

java

@SuppressWarnings("rawtypes")

public class HashMapDemo {

//hashMap遍历

public static void main(String[] args) {

Map<String, String> map = new HashMap<>();

map.put("k1", "v1");

map.put("k2", "v2");

map.put("k3", "v3");

//方法一

Iterator it = map.entrySet().iterator();

while(it.hasNext()){

Map.Entry m = (Map.Entry)it.next();

System.out.println("key=" + m.getKey() + "," + "value=" + m.getValue());

}

System.out.println();

//方法二

for(Map.Entry<String, String> m : map.entrySet()){

System.out.println("key=" + m.getKey() + "," + "value=" + m.getValue());

}

System.out.println();

//方法三

Iterator iterator = map.keySet().iterator();

while(iterator.hasNext()){

String key = (String)iterator.next();

System.out.println("key=" + key + "," + "value=" + map.get(key));

}

}

}

以上是 170530、java 迭代hashmap常用的三种方法 的全部内容, 来源链接: utcz.com/z/392594.html

回到顶部