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

遍历集合
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 setIteratorRemove() {
		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);  
        }  
	}
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值