本文主要介绍Java中,使用for循环或while循环来遍历HashMap的方法,以及相关示例代码。

1、使用entrySet()遍历

相关文档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<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}

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

相关文档:

values()

keySet()

1) 使用keySet()遍历key

Collection<?> keys = map.keySet();
for(Object key: keys){
System.out.println("Key " + key);
System.out.println("Value " + map.get(key));
}

2)使用values()遍历值

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
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<String, String> 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<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key + "," + value);
		}
		System.out.println("=========================");
		List<String> removeKeys = new ArrayList<String>();
		for (Entry<String, String> entry : map.entrySet()) {
			if (!entry.getValue().equals("1")) {
				removeKeys.add(entry.getKey());
			}
		}
		for (String removeKey : removeKeys) {
			map.remove(removeKey);
		}
		for (Entry<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key + "," + value);
		}
		System.out.println("=========================");
	}
}

相关文档:

Java使用hashmap中的数据初始化hashmap并执行put数据的简洁代码

Java hashmap computeIfAbsent()使用方法及示例代码

Java中 static final修饰hashmap静态成员变量初始化方法