我们都知道你不能这样做:
for (Object i : l) {
if (condition(i)) {
l.remove(i);
}
}
Run Code Online (Sandbox Code Playgroud)
ConcurrentModificationException等等......这显然有时起作用,但并非总是如此.这是一些特定的代码:
public static void main(String[] args) {
Collection<Integer> l = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
l.add(4);
l.add(5);
l.add(6);
}
for (int i : l) {
if (i == 5) {
l.remove(i);
}
}
System.out.println(l);
}
Run Code Online (Sandbox Code Playgroud)
当然,这会导致:
Exception in thread "main" java.util.ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud)
...即使多线程没有这样做......无论如何.
什么是这个问题的最佳解决方案?如何在循环中从集合中删除项而不抛出此异常?
我也在Collection这里使用任意,不一定是ArrayList,所以你不能依赖get.
我有以下代码:
private String toString(List<DrugStrength> aDrugStrengthList) {
StringBuilder str = new StringBuilder();
for (DrugStrength aDrugStrength : aDrugStrengthList) {
if (!aDrugStrength.isValidDrugDescription()) {
aDrugStrengthList.remove(aDrugStrength);
}
}
str.append(aDrugStrengthList);
if (str.indexOf("]") != -1) {
str.insert(str.lastIndexOf("]"), "\n " );
}
return str.toString();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到ConcurrentModificationException,任何人都可以解释它为什么会发生,即使代码在同一个线程中运行?我怎么能避免它呢?
@Test
public void testListCur(){
List<String> li=new ArrayList<String>();
for(int i=0;i<10;i++){
li.add("str"+i);
}
for(String st:li){
if(st.equalsIgnoreCase("str3"))
li.remove("str3");
}
System.out.println(li);
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我将抛出一个ConcurrentModificationException.
看起来当我从列表中删除指定的元素时,列表不知道它的大小是否已被更改.
我想知道这是集合和删除元素的常见问题吗?
我正在迭代一个JRE Collection,它强制执行fail-fast迭代器概念.问题是如果对象满足条件,我需要删除对象的逻辑伙伴.从而防止合作伙伴被处理.有人可以为此建议更好的收集类型吗?
例.
myCollection<BusinessObject>
for (BusinessObject anObject : myCollection)
{
if (someConditionIsTrue)
{
myCollection.remove(anObjectsPartner); // throws ConcurrentModificationException
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
这是我的代码:
// eventList is a LinkedList
public void run() {
Iterator<Event> it = eventList.iterator();
int size = eventList.size();
while(size > 0) {
while(it.hasNext()) {
Event e = it.next(); //flaged line
if(e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
--size;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误java.util.ConcurrentModificationException是在flag lined(Event e = it.next();)处抛出的.你是否在我的代码中看到一个错误,这个错误明显是抛出异常的原因?
我有一个Server类和一个Timer,它应该清除死客户端(崩溃的客户端).我按照下面的示例通过在Timer迭代用户时锁定集合但我仍然得到此异常(在我崩溃连接的客户端之后).
http://www.javaperformancetuning.com/articles/fastfail2.shtml
List<User> users;
List<User> connectedUsers;
ConcurrentMap<User, IClient> clients;
...
users = Collections.synchronizedList(new ArrayList<User>());
connectedUsers = new ArrayList<User>();
clients = new ConcurrentHashMap<User, IClient>();
timer = new Timer();
timer.schedule(new ClearDeadClients(), 5000, 5000);
...
class ClearDeadClients extends TimerTask {
public void run() {
synchronized (users) {
Iterator<User> it = users.iterator();
while (it.hasNext()) {
User user = it.next(); // Throws exception
if (!connectedUsers.contains(user)) {
users.remove(user);
clients.remove(user);
}
}
}
connectedUsers.clear();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个List,我想循环遍历该List并在某些条件下删除一些记录库.这就是我的工作
public void foo(List<Bar> recordList){
for(Bar bar : recordList){
if(bar.someCondition()){
recordList.remove(bar);
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码生成异常.如果我使用Iterator,那么它工作正常
public void foo(List<Bar> recordList){
Iterator<Bar> iter = recordList.iterator();
while(iter.hasNext()){
Bar bar = iter.next();
if(bar.someCondition()){
iter.remove();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我猜我的问题:
为什么第一段代码不起作用?
如何使第一段代码工作?
这个小小的烦恼导致我失去了一个小时的睡眠,我不明白为什么.
我有一个ArrayList数组,我想迭代并有条件地删除项目.这是我的第一次尝试:
for (int i = 0; i < array.size(); i++) {
if (array.get(i) == conditionMet) array.remove(i);
}
Run Code Online (Sandbox Code Playgroud)
那没用.以下做了:
for (Iterator<T> i = array.iterator(); i.hasNext();) {
if (i.next() == conditionMet) i.remove();
}
Run Code Online (Sandbox Code Playgroud)
为什么?
我有一个List,如果匹配,我将循环删除列表中的项目.如果列表中的项目被删除,我正在使用i = -1.但它又从头开始循环.有一个更好的方法吗?
private List<String> populateList(List<String> listVar) {
List<String> list = new ArrayList<String>();
list.add("2015-01-13 09:30:00");
list.add("2015-01-13 06:22:12");
list.add("2015-01-12 05:45:10");
list.add("2015-01-12 01:52:40");
list.add("2015-01-12 02:23:45");
return list;
}
private void removeItems() {
List<String> list = new ArrayList<String>();
list = populateList(list);
System.out.println("List before modification : "+list);
for (int i = 0; i < list.size(); i++) {
String dateNoTime = list.get(i).split(" ")[0];
System.out.println(" Going over : "+list.get(i));
if(!dateNoTime.equalsIgnoreCase("2015-01-13")) {
System.out.println(" Removing : "+list.get(i));
list.remove(i);
i = -1; //This is making the loop start from …Run Code Online (Sandbox Code Playgroud) java ×9
collections ×4
arraylist ×2
list ×2
client ×1
concurrency ×1
exception ×1
iteration ×1
linked-list ×1