本帖最后由 Piaget 于 2015-2-11 00:48 编辑
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.HashSet;
- import java.util.Set;
- public class TestRemove
- {
- public static void main(String[] args)
- {
- Set<String> hsSet = new HashSet<String>();
- hsSet.add("abc");
- hsSet.add("aabac");
- hsSet.add("abc");
- hsSet.add("afc");
- Iterator<String> iterator = hsSet.iterator();
- while (iterator.hasNext())
- {
- if (iterator.next().equals("abc"))
- {
- hsSet.remove(iterator.next());//为什么这里没有并发异常呢???
- }
- }
- System.out.println(hsSet);
- List<String> list = new ArrayList<String>();
- list.add("abc");
- list.add("sd");
- list.add("abc");
- list.add("oc");
- /*Iterator<String> it = list.iterator();
- while (it.hasNext())
- {
- if (it.next().equals("abc"))
- {
- list.remove("abc");//这里会有并发异常。
- }
- }*/
- System.out.println(list);
- }
-
- }
复制代码 |
|