I have a normal Database call that gathers information from my database. i use these informations to create my objects (CallQueue
) these objects are then added to a list and the list is then returned.
Suddenly i discovered that my originally code did not work as intended because i created dublicates and so now i am trying to void that any dublicates are being created! But there is a problem!
I am unable to loop through my list and check wether or not the object is already created!
Here is my code:
while (query.next()) {
if (!queues.isEmpty()) {
/*This gives the Execption->*/
for (CallQueue callQueue : queues) {
if (callQueue.getType().equals(query.getString("KØ"))) {
double decimalTime = query.getDouble("TID");
int hourOfDay = (int)Math.round(24 * decimalTime);
int callAmount = query.getInteger("ANTAL_KALD");
if (hourOfDay > 19) {
hourOfDay = 19;
}
callQueue.addCallsByTime(hourOfDay, callAmount);
} else {
String queueName = query.getString("Kø");
if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
CallQueue cq = new CallQueue(query.getString("KØ"));
double decimalTime = query.getDouble("TID");
int hourOfDay = (int)Math.round(24 * decimalTime);
int callAmount = query.getInteger("ANTAL_KALD");
if (hourOfDay > 19) {
hourOfDay = 19;
}
cq.addCallsByTime(hourOfDay, callAmount);
queues.add(cq);
}
}
}
} else {
String queueName = query.getString("Kø");
if (!queueName.equalsIgnoreCase("PrivatOverflow")) {
CallQueue cq = new CallQueue(query.getString("KØ"));
double decimalTime = query.getDouble("TID");
int hourOfDay = (int)Math.round(24 * decimalTime);
int callAmount = query.getInteger("ANTAL_KALD");
if (hourOfDay > 19) {
hourOfDay = 19;
}
cq.addCallsByTime(hourOfDay, callAmount);
queues.add(cq);
}
}
}
for (CallQueue callQueue : queues) {
System.out.println(callQueue.getType());
}
query.Close();
return queues;
The execption i get from this is:
Caused by: java.util.ConcurrentModificationException
ive tried to look up the execption at ConcurrentModificationException
Can anyone help me fix this problem?
You're doing an add inside of your iteration. As per the spec, you're not allowed to modify the collection you're iterating over.
The classic solution is to make a copy of the collection first and iterate over that instead. Another solution is to not use the iterator (the short foreach notation is using it implicitly) but to manually iterate using an index.
for (int i=0; i<queues.size(); i++) {
CallQueue callQueue = queues.get(i);
... code goes here
}
A better solution would be to use a Set instead of a list (unless the order is important for you). That does mean you would have to implement equals and hashcode properly.
Btw: I believe your code is flawed. You're iterating over your list, and if the item you encounter does not match, you add one at the end. Meaning that, if the item you're looking for is the xth one in the list, you will have added a new item x times. I seriously doubt that is what you need. If you do some refactoring, this will become immediately clear.