您的位置:首页 > 其它

List,Set,Map遍历时删除元素

2017-09-14 10:50 447 查看
遍历集合
0)使用普通for循环遍历
1)若使用增强for循环来遍历若有修改删除操作会抛出java.util.ConcurrentModificationException
2)使用迭代器遍历

List的特性:可以通过下标来获取元素和删除元素.

故list的遍历和删除多元素可以使用普通for循环

Map和Set不可通过下标获取元素和删除元素

故Map只能通过增强for循环遍历,或者通过迭代器来遍历

迭代器:iterator
1)使用iterator来遍历可以删除
2)iterator只可用作遍历和删除,没有增加和修改的方法

List操作

package collectionDel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;

/**
* @author MrLeeYongSheng
* 遍历List的三种方法:
* 1)for:问题:每次删除后后面的元素会向前移动
* 2)增强for循环:问题:抛并行访问修改操作异常java.util.ConcurrentModificationException
* 3)迭代器Iterator:没问题
*/
public class ListRemove {

/**
* 普通for循环
*/
@Test
public void listForRemove() {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
for(int i=0; i<list.size(); i++) {
list.remove(1);
list.remove(2);
}
printList(list);
/*抛异常
* java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
* 原因,当删除了下标[1]元素,下标[2]元素会向前移动
*/
}
//试试LinkedList吧,结果同上

/**
* 增强for循环
*/
@Test
public void listForeRemove() {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
for(String a : list) {
list.remove(a);
}
printList(list);
/*抛异常
java.util.ConcurrentModificationException
*/
}

/**
* 迭代器Iterator
*/
@Test
public void listIteratorRemove() {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
String next = iterator.next();
if("b".equals(next))
iterator.remove();
}
printList(list);
/*抛异常
java.util.ConcurrentModificationException
*/
}
private void printList(List<String> list) {
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
}
}

Set操作

package collectionDel;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

/**
* @author MrLeeYongSheng
*	使用增强for循环遍历删除:抛java.util.ConcurrentModificationException
*	使用iterator便利删除:没问题
*/
public class SetRemove {

Set<String> set;
@Before
public void beforeClass() {
set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
}

/**
* 遍历增强for循环遍历set删除
*/
@Test
public void setRemove() {
for (String v : set) {
set.remove(v);
}
printSet(set);
//抛java.util.ConcurrentModificationException
}

/**
* 使用Iterator遍历set来删除
*/
@Test
public void setItera
a98c
torRemove() {
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()) {
String next = iterator.next();
if("b".equals(next))
iterator.remove();
}
printSet(set);
/*
c
a
*/
}
private void printSet(Set<String> set) {
for (String string : set) {
System.out.println(string);
}
}
}


Map操作

package collectionDel;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

/**
* @author MrLeeYongSheng
*	遍历map的方法有两种:
*		1:通过keySet
*		2:通过entrySet
*  删除也有两种:
*  	1:map.remove(key):会抛java.util.ConcurrentModificationException
*  	2:iterator.remove():没问题
*/
public class MapRemove {

Map<String, String> map;

@Before
public void beforeClass() {
map = new HashMap<String, String>();
map.put("a", "a");
map.put("b", "b");
map.put("c", "c");
}

/**
* 遍历keySet的方法来删除
*/
@Test
public void mapKeySetRemove() {

Set<String> keySet = map.keySet();
for (String key : keySet) {
map.remove(key);
//keySet.remove(key);
}
printMap(map);

/*抛异常
java.util.ConcurrentModificationException
* */
}

/**
* 使用迭代器Iterator遍历keySet的方法来删除
*/
@Test
public void mapKeySetIteratorRemove() {
Set<String> keySet = map.keySet();
Iterator<String> setIterator = keySet.iterator();
while(setIterator.hasNext()) {
String obj = setIterator.next();
if("b".equals(obj))
setIterator.remove();
}
printMap(map);
/*
c==c
a==a
* */
}
/**
* 遍历EntrySet来删除
*/
@Test
public void mapEntrySetRemove() {
Set<Entry<String,String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
String key = entry.getKey();
//entrySet.remove(entry);
map.remove(key);
}
printMap(map);
/*抛异常
java.util.ConcurrentModificationException
* */
}

/**
* 使用Iterator来遍历entrySet来删除
*/
@Test
public void mapEntrySetIteratorRemove() {
Set<Entry<String,String>> entrySet = map.entrySet();
Iterator<Entry<String, String>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String, String> next = iterator.next();
if("b".equals(next.getKey()))
iterator.remove();
}
printMap(map);
/*
c==c
a==a
*/
}

private void printMap(Map<String,String> map) {
/*Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key+"=="+map.get(key));
}*/
for(Map.Entry<String, String> entry:map.entrySet()){
String k=entry.getKey();
String v=entry.getValue();
System.out.println(k+"=="+v);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: