java for遍历hashmap_Java 使用for和while循环遍历HashMap的方法及示例代码

1、使用entrySet()遍历

1) 使用while实现public static void printMap(Map mp) {

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

while (it.hasNext()) {

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

System.out.println(pair.getKey() + " = " + pair.getValue());

it.remove(); // avoids a ConcurrentModificationException

}

}

2) 使用for实现for (Map.Entry entry : map.entrySet()) {

String key = entry.getKey();

Object value = entry.getValue();

// ...

}

2、使用for和keySet()或values()实现

相关文档:

1) 使用keySet()遍历keyCollection> keys = map.keySet();

for(Object key: keys){

System.out.println("Key " + key);

System.out.println("Value " + map.get(key));

}

2)使用values()遍历值Map map = new HashMap();

for (Integer value : map.values()) {

System.out.println("Value = " + value);

}

3、HashMap使用示例代码import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map.Entry;

public class HashMapTest {

public static void main(String[] args) {

HashMap map = new HashMap<>();

map.put("java", "31");//存放键值对

System.out.println(map.containsKey("java"));//键中是否包含这个数据

System.out.println(map.containsKey("python"));

System.out.println("=========================");

System.out.println(map.get("java"));//通过键拿值

System.out.println(map.get("python"));

System.out.println("=========================");

System.out.println(map.isEmpty());//判空

System.out.println(map.size());

System.out.println("=========================");

System.out.println(map.remove("java"));//从键值中删除

System.out.println(map.containsKey("python"));

System.out.println(map.get("javapy"));//获取值

System.out.println(map.isEmpty());

System.out.println(map.size());

System.out.println("=========================");

map.put("cjavapy", "31");

System.out.println(map.get("cjavapy"));

map.put("cjavapy", "32");

System.out.println(map.get("cjavapy"));

System.out.println("=========================");

map.put("cjavapy", "1");

map.put("javapy", "2");

map.put("python", "3");

for (String key : map.keySet()) {

System.out.println(key);

}

System.out.println("=========================");

for (String values : map.values()) {

System.out.println(values);

}

System.out.println("=========================");

map.clear();

map.put("A", "1");

map.put("B", "2");

map.put("C", "3");

map.put("D", "1");

map.put("E", "2");

map.put("F", "3");

map.put("G", "1");

map.put("H", "2");

map.put("I", "3");

for (Entry entry : map.entrySet()) {

String key = entry.getKey();

String value = entry.getValue();

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

}

System.out.println("=========================");

List removeKeys = new ArrayList();

for (Entry entry : map.entrySet()) {

if (!entry.getValue().equals("1")) {

removeKeys.add(entry.getKey());

}

}

for (String removeKey : removeKeys) {

map.remove(removeKey);

}

for (Entry entry : map.entrySet()) {

String key = entry.getKey();

String value = entry.getValue();

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

}

System.out.println("=========================");

}

}

相关文档:

你可能感兴趣的:(java,for遍历hashmap)