当前位置:   article > 正文

HashMap集合的3种迭代方式_map param = new hashmap();

map param = new hashmap();
package teat;

import java.util.*;
import java.util.Map.Entry;

/**
 * @author linjitai
 */
public class ThreeIterator {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("001", "宝马");
map.put("002", "奔驰");
map.put("003", "奥迪");
firstIterator(map);
System.out.println("===");
twoIterator(map);
System.out.println("===");
threeItertor(map);
}
/**
* entry
* @param map
* @return
*/
public static void firstIterator(Map<String, String> map) {
for (Entry<String, String> entry : map.entrySet()) {
String keys = entry.getKey();
String values = entry.getValue();
System.out.println("key = "+ keys + ";value = "+ values);
}
}
/**
* keySet
* @param map
*/
public static void twoIterator(Map<String, String> map) {
for (String keys : map.keySet()) {
String values = map.get(keys);
System.out.println("key = "+ keys + ";value = "+ values);
}
}
/**

* @param map
*/
public static void threeItertor(Map<String, String> map) {
Iterator<String> keys = map.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
String value = map.get(key);
System.out.println("key = "+ key + ";value = "+ value);
}
}
}
/**

Output

key = 001;value = 宝马
key = 002;value = 奔驰
key = 003;value = 奥迪
===
key = 001;value = 宝马
key = 002;value = 奔驰
key = 003;value = 奥迪
===
key = 001;value = 宝马
key = 002;value = 奔驰
key = 003;value = 奥迪

*/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/460864
推荐阅读
相关标签
  

闽ICP备14008679号