Java Map遍历的同时进行元素删除
2015-02-06 21:54
483 查看
代码如下:
import static com.hutao.util.Print.println;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> map = new TreeMap<String, String>();
map.put("name", "user");
map.put("password", "12345");
Set<Map.Entry<String, String>> set = map.entrySet();
println(set.toString());
Set<Map.Entry<String, String>> tempSet = new HashSet(set);
for (Map.Entry<String, String> en : tempSet) {
set.remove(en);
}
println(set.toString());
}
}
注意:容器在遍历的同时,是不能对元素进行删除 ,否则会报异常,这里介绍只是一种讨巧的方法
import static com.hutao.util.Print.println;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> map = new TreeMap<String, String>();
map.put("name", "user");
map.put("password", "12345");
Set<Map.Entry<String, String>> set = map.entrySet();
println(set.toString());
Set<Map.Entry<String, String>> tempSet = new HashSet(set);
for (Map.Entry<String, String> en : tempSet) {
set.remove(en);
}
println(set.toString());
}
}
注意:容器在遍历的同时,是不能对元素进行删除 ,否则会报异常,这里介绍只是一种讨巧的方法
相关文章推荐
- Java中在对Map进行遍历的同时修改Map元素
- java 遍历map同时删除元素
- java遍历Map时remove删除元素
- java遍历Map时remove删除元素
- Java 遍历Map时 删除元素
- Java Map遍历过程中删除元素
- 【Java】集合(List、Set、Map)遍历、删除、比较元素时的小陷阱
- Java中遍历Map时删除元素的方法
- java 遍历map时删除元素
- Java 遍历Map时 删除元素
- Java 遍历Map时 删除元素
- java.util.ConcurrentModificationException---用迭代器遍历Map时删除其中元素出现异
- Java 遍历Map时 删除元素
- Java 遍历Map时 删除元素ZZ 分类: Android资源 2014-05-30 10:59 74人阅读 评论(0) 收藏
- Java 遍历Map时 删除元素
- java遍历Map时remove删除元素
- Java&nbsp;遍历Map时&nbsp;删除元素ZZ
- java遍历map并删除元素时,使用不当会抛弃异常
- java遍历Map时remove删除元素 分类: Android开发 2014-05-30 10:58 84人阅读 评论(0) 收藏
- Java 遍历Map时 删除元素