欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hashMap遍历删除元素

程序员文章站 2022-06-04 18:56:28
...
package test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Test {

	public static void main(String[] args) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("1", "a");
		map.put("2", "b");
		map.put("3", "c");
		map.put("4", "d");
		
		
		 for (Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext(); ) {
	            Map.Entry<String, String> item = it.next();
	            String abc = Integer.toHexString(item.hashCode());
	            it.remove();
	        }
		
		for (Entry<String, String> entry : map.entrySet()) {
			System.out.println("key"+entry.getKey() +"  value:"+entry.getValue());
		}
	}

}