使用Iterate
List list = new ArrayList();
list.add("a");list.add("b");list.add("c");
//for结构 自动生成
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String s = (String) iterator.next();
if(s.equals("b")){
iterator.remove();
}
}
System.out.println(list.size());
以下代码会抛出异常
package tristan;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String, String>();
map.put("1", "a1");
map.put("2", "a2");
map.put("3", "a3");
map.put("4", "a4");
map.put("5", "a5");
for(String key : map.keySet()){
if("a2".equals(map.get(key))){
map.remove(key);
}
}
System.out.println(map.size());
}
}
应该深拷贝一个tmpMap,在迭代的过程中,对tmpMap进行remove操作
之后将map 赋值为 tmpMap.
package tristan;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws Exception {
Map<String,String> map = new HashMap<String, String>();
map.put("1", "a1");
map.put("2", "a2");
map.put("3", "a3");
map.put("4", "a4");
map.put("5", "a5");
Map<String,String> tmpMap = (Map<String, String>) clone(map);
for(String key : map.keySet()){
if("a2".equals(map.get(key))){
tmpMap.remove(key);
}
}
map = tmpMap;
System.out.println(map.size());
}
public static Object clone(Object obj) throws Exception {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(byteOut);
outStream.writeObject(obj);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream inStream = new ObjectInputStream(byteIn);
return inStream.readObject();
}
}
但是这种写法可能有未知的问题, 就像四国中 灭掉一家时出现的问题一样。
最好是用下列方法
package tristan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) throws Exception {
Map<String,String> map = new HashMap<String, String>();
map.put("1", "a1");
map.put("2", "a2");
map.put("3", "a3");
map.put("4", "a4");
map.put("5", "a5");
List<String> delList = new ArrayList<String>();
for(String key : map.keySet()){
if("a2".equals(map.get(key))){
delList.add(key);
}
}
for(String key : delList){
map.remove(key);
}
System.out.println(map.size());
}
}
谢谢latent同学的建议,最好还是采用Iterate来做,List Set也适用
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) throws Exception {
testMap();
}
public static void testSet() {
Set<String> set = new HashSet<String>();
set.add("a1");
set.add("a2");
set.add("a3");
set.add("a4");
set.add("a5");
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String v = it.next();
if ("a2".equals(v)) {
it.remove();
}
}
for (String v : set) {
System.out.println(v);
}
}
public static void testList() {
List<String> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
list.add("a3");
list.add("a4");
list.add("a5");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String v = it.next();
if ("a2".equals(v)) {
it.remove();
}
}
for (String v : list) {
System.out.println(v);
}
}
public static void testMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "a1");
map.put("2", "a2");
map.put("3", "a3");
map.put("4", "a4");
map.put("5", "a5");
/* Set<Entry<String, String>> set = map.entrySet();
Iterator<Entry<String, String>> it = set.iterator();
while (it.hasNext()) {
String v = it.next().getValue();
if (v.equals("a2")) {
it.remove();
}
} */
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String k = it.next();
if (map.get(k).equals("a2")) {
it.remove();
}
}
for (Map.Entry<String, String> e : map.entrySet()) {
System.out.println(e.getValue());
}
}
}