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());
}
}
}
上一篇: mongodb的写操作