hashMap遍历删除元素

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());
		}
	}

}

猜你喜欢

转载自blog.csdn.net/sinat_37795871/article/details/88696038