if have the following problem:
I have a List which i am going through using the enhanced for loop. Every time i want to remove sth, out of the list, i get a ConcurrentModificationException. I already found out why this exception is thrown, but i don`t know how i can modify my code, so that its working. This is my code:
for(Subject s : SerData.schedule)
{
//Checking of the class is already existing
for(Classes c : s.classes)
{
if(c.day == day &c.which_class == which_class)
{
int index = getclassesindex(s.classes, new Classes(day, which_class));
synchronized (s) {
s.classes.remove(index);
}
}
}
//More code....
}
I also tried out this implementation.
for(Subject s : SerData.schedule)
{
//Checking of the class is already existing
Iterator<Classes> x = s.classes.iterator();
while(x.hasNext())
{
Classes c = x.next();
if(c.day == day &c.which_class == which_class)
{
int index = getclassesindex(s.classes, new Classes(day, which_class));
synchronized (s) {
s.classes.remove(index);
}
}
}
//More code....
}
not working either...
Is there a common used, standard solution? (Hopefully sth. that is not obvious :D )
The main reason this issue occurs is because of the semantic meaning of your for-each loop.
When you use for-each loops, the data structure that is being traversed cannot be modified.
Essentially anything of this form will throw this exception:
for( Object o : objCollection )
{
// ...
if ( satisfiesSomeProperty ( o ) )
objList.remove(o); // This is an error!!
// ...
}
As a side note, you can't add or replace elements in the collection either.
There are a few ways to perform this operation.
One way is to use an iterator and call the remove() method when the object is to be removed.
Iterator <Object> objItr = objCollection.iterator();
while(objItr.hasNext())
{
Object o = objItr.next();
// ...
if ( satifiesSomeProperty ( o ) )
objItr.remove(); // This is okay
// ...
}
This option has the property that removal of the object is done in time proportional to the iterator's remove method.
The next option is to store the objects you want to remove, and then remove them after traversing the list. This may be useful in situations where removal during iteration may produce inconsistent results.
Collection <Object> objsToRemove = // ...
for( Object o : objCollection )
{
// ...
if ( satisfiesSomeProperty ( o ) )
objsToRemove.add (o);
// ...
}
objCollection.removeAll ( objsToRemove );
These two methods work for general Collection types, but for lists, you could use a standard for loop and walk the list from the end of the list to the front, removing what you please.
for (int i = objList.size() - 1; i >= 0; i--)
{
Object o = objList.get(i);
// ...
if ( satisfiesSomeProperty(o) )
objList.remove(i);
// ...
}
Walking in the normal direction and removing could also be done, but you would have to take care of how incrementation occurs; specifically, you don't want to increment i when you remove, since the next element is shifted down to the same index.
for (int i = 0; i < objList.size(); i++)
{
Object o = objList.get(i);
// ...
if ( satisfiesSomeProperty(o) )
{
objList.remove(i);
i--;
}
//caveat: only works if you don't use `i` later here
// ...
}
Hope this provides a good overview of the concepts and helps!
Using Iterator.remove() should prevent the exception from being thrown.
Hm if I get it right you are iterating over a collection of classes and if a given class matches some criteria you are looking for the its index and try to remove it?
Why not just do:
Iterator<Classes> x = s.classes.iterator();
while(x.hasNext()){
Classes c = x.next();
if(c.day == day && c.which_class == which_class) {
x.remove();
}
}
Add synchronization if need be (but I would prefer a concurrent collection if I were you), preferably change the "==" to equals(), add getters/setters etc. Also the convention in java is to name variables and methods using camelCase (and not separating them with "_").
Actually this is one of the cases when you have to use an iterator.
From the javadoc on ConcurrentModificationException:
"if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception."
So within your
for (Classes c : s.classes)
you are executing
s.classes.remove(index)
and the iterator is doing just what its contract says. Declare the index(es) in a scope outside the loop and remove your target after the loop is done.
Iterator<Classes> classesIterator = s.classes.iterator();
while (classesIterator.hasNext()) {
Classes c = classesIterator.next();
if (c.day == day && c.which_class == which_class) {
classesIterator.remove();
}
}
There is no general solution for Collection subclasses in general - most iterators will become invalid if the collection is modified, unless the modification happens through the iterator itself via Iterator.remove().
There is a potential solution when it comes to List implementations: the List interface has index-based add/get/set/remove operations. Rather than use an Iterator instance, you can iterate through the list explicitly with a counter-based loop, much like with arrays. You should take care, however, to update the loop counter appropriately when inserting or deleting elements.
Your for-each iterator is fail-fast and this is why remove operation fails as it would change the collection while traversing it.
What implementation of List interface are you using?
Noticed synchronisation on Subject, are you using this code concurrently?
If concurrency is the case, then I would recommend using CopyOnWriteArrayList. It doesn't need synchronisation and its for-each iterator doesn't throw ConcurrentModificationException.
Related
I'm using
Collections.synchronizedList(new ArrayList<T>())
part of the code is:
list = Collections.synchronizedList(new ArrayList<T>());
public void add(T arg) {
int i;
synchronized (list) {
for (i = 0; i < list.size(); i++) {
T arg2 = list.get(i);
if (arg2.compareTo(arg) < 0) {
list.add(i, arg);
break;
}
}
Is it right that for loop is actually using iterator and therefore I must wrap the for with synchronized?
Is it thread-safe to use synchronized and make addition inside it like I did here?
I'm sorry if these questions are very basic, I'm new to the subject and didn't find answers on the internet.
Thank you!!
Is it right that for loop is actually using iterator and therefore I must wrap the for with synchronized?
There are two parts to your question.
Firstly, no, you're not using an iterator here, this is a basic for loop.
The enhanced for loop is the for loop which uses an iterator:
for (T element : list) { ... }
You can see in the language spec how this uses the iterator - search for where it says "The enhanced for statement is equivalent to a basic for statement of the form".
Secondly, even though you're not using an iterator, you do need synchronized. The two are orthogonal.
You are doing multiple operations (the size, the get and the add), with dependencies between them. You need to make sure that no other thread interferes with your logic:
the get depends on the size, since you don't want to try to get an element with index >= size, for instance;
the add depends on the get, since you're apparently trying to ensure the list elements are ordered. If another thread could sneak in and change the element after you get it, you might insert the new element in the wrong place.
You correctly avoid this potential interference this through synchronization over list, and creating the synchronizedList in such a way that nothing other than the synchronizedList can get direct access to the underlying list.
If your arg2.compareTo(arg) never return 0 (zero) you can use TreeSet. Will be much more simple:
set = Collections.synchronizedSet(new TreeSet<T>());
public void add(T arg) {
set.add(arg);
}
If you need hold same items (compareTo returns 0) then use the list:
list = new ArrayList<T>();
public void add(T arg) {
synchronized (list) {
int index = Collections.binarySearch(list, arg);
list.add(index, arg);
}
}
First and second cases complexity will be log(N) (10 for 1000 items). Your code complexity is N (1000 for 1000 items).
In Java it is possible to declare a variable in the initialization part of a for-loop:
for ( int i=0; i < 10; i++) {
// do something (with i)
}
But with the while statement this seems not to be possible.
Quite often I see code like this, when the conditional for the while loop needs to be updated after every iteration:
List<Object> processables = retrieveProcessableItems(..); // initial fetch
while (processables.size() > 0) {
// process items
processables = retrieveProcessableItems(..); // update
}
Here on stackoverflow I found at least a solution to prevent the duplicate code of fetching the processable items:
List<Object> processables;
while ((processables = retrieveProcessableItems(..)).size() > 0) {
// process items
}
But the variable still has to be declared outside the while-loop.
So, as I want to keep my variable scopes clean, is it possible to declare a variable within the while conditional, or is there some other solution for such cases?
You can write a while loop using a for loop:
while (condition) { ... }
is the same as
for (; condition; ) { ... }
since all three bits in the brackets of the basic for statement declaration are optional:
BasicForStatement:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
Similarly, you can just rewrite your while loop as a for loop:
for (List<Object> processables;
(processables = retrieveProcessableItems(..)).size() > 0;) {
// ... Process items.
}
Note that some static analysis tools (e.g. eclipse 4.5) might demand that an initial value is assigned to processables, e.g. List<Object> processables = null. This is incorrect, according to JLS; my version of javac does not complain if the variable is left initially unassigned.
No it's not possible.
It doesn't really make too much sense either: unlike a for loop where you can set up the initial state of the "looping variable", in a while loop you test the value of an existing variable, akin to the conditional check of the for loop.
Of course, if you're concerned about variables "leaking" into other parts of your code, you could enclose the whole thing in an extra scope block:
{
/*declare variable here*/
while(...){...}
}
Alternatively, convert the while loop into a for loop.
Make a do/while:
String value;
do {
value = getValue();
...your processing
} while (value != null && !value.isEmpty());
In the conventional loop, we could have as below, making single nested layer.
for (int i=0; listObject != null && i < listObject.size(); i++) {
// Do whatever we want
}
However, using the below style for each loop, I'll need a double nested code: -
if (listObject != null) {
for (Object object: listObject) {
// Do whatever we want
}
}
Is it possible to embed the listObject != null condition into the for-loop statement to make it single nested code?
Your second example is clear, easily understood code. There is nothing wrong with nesting a for loop in an if block. It's even more clear than your first example.
But if you insist on combining them, you can use the ternary operator to supply an empty list if listObject is null. Using Collections.emptyList means no iterations will take place and no NullPointerException will be thrown.
for (Object object : listObject == null ? Collections.emptyList() : listObject)
I don't think I would use code like this when a clear example such as your second example already exists, but this code does provide a way to get two colons inside a for loop.
To make it concise on, while having a single nested loop, I decided to make it into function as below
void checkCondition(List<Object> listObject) {
if (listObject == null) return;
for (Object object: listObject) {
// Do whatever
}
}
public void putExpensiveWineCaseBack(double notBiggerThan)
{
Iterator<WineCase> it = basket.iterator();
WineCase checkedWineCase = null;
while( it.hasNext() )
{
checkedWineCase = it.next();
double checkedPrice = checkedWineCase.getPrice();
int i=0;
for(i=0; i<basket.size(); i++)
{
if(checkedPrice > notBiggerThan)
{
basket.remove(i);
}
}
}
}
}
This code is compiling. The problem is that when executed I get this error:
java.util.ConcurentModificationexception:
null(in java.util.ArrayList$Itr)
for this line:
checkedWineCase = it.next();
What am I missing ?
Change from
basket.remove(i);
to
it.remove();
You're calling basket.remove() but should call it.remove() instead.
You are removing items from the list in the loop where you are iterating through the items in the list. That is the cause of the ConcurrentModificationException.
'null pointer' does not exactly describe your problem, rather it is the name of the exception you received: ConcurentModificationexception
When an iterator is instantiated, it provides access to a certain group of objects, in this case, the contents of 'basket'. It might take some time to go from getting the first object in the iterator to getting the last one.
Now, what happens if the objects in the 'basket' change? The iterator is designed to be 'fail-fast', meaning if the basket has changed, you will immediately get an exception the next time you try to use the Iterator.
This is happening because in some cases you call 'basket.remove()' before you have finished iterating over everything in the basket. You may wish to 'remember' which things should be removed from the basket, and then remove them only when you are completely done with the iterator.
Google 'java iterator concurrentmodificationexception' to see many more explanations of the issue you are encountering.
The problem is you are removing elements from basket while you are iterating over it.
Actually I'm not sure what you are trying to do, as that for loop makes not much sense to me. But I guess you want to remove the element from the Set if the price is larger than notBiggerThan.
So maybe you should try like this:
while(it.hasNext()) {
checkedWineCase = it.next();
double checkedPrice = checkedWineCase.getPrice();
if(checkedPrice > notBiggerThan)
{
it.remove();
}
}
You should not modify the collection while iterating, except by way of Iterator.remove().
The problem comes from your call to basket.remove(i);
I do not completely understand what you are trying to do with that inner for loop (currently it looks like it will remove everything from your basket if any have a price > than the maximum), maybe you want the following:
public void putExpensiveWineCaseBack(double notBiggerThan)
{
Iterator<WineCase> it = basket.iterator();
WineCase checkedWineCase = null;
while( it.hasNext() )
{
checkedWineCase = it.next();
double checkedPrice = checkedWineCase.getPrice();
if(checkedPrice > notBiggerThan)
{
it.remove();
}
}
}
I am attempting to write code to traverse a collection of type InstallationComponentSetup:
java.util.Collection<InstallationComponentSetup> components= context.getInstallationComponents();
Iterator it = components.iterator();
while (it.hasNext())
{
if (((InstallationComponentSetup)it).getName() == "ACQ")
{
return true;
}
}
The cast in the if-statement fails, but I don't really know why (I am a C++ programmer!).
If someone could give me some pointers as to what I am doing wrong I would be grateful.
it is an Iterator, whereas it.next() is an InstallationComponentSetup.
The error results from the fact that an Iterator cannot be cast as an InstallationComponentSetup.
Also, you shouldn't even need to cast if you parametrize the Iterator appropriately:
Iterator<InstallationComponentSetup> it = components.iterator();
Finally, don't compare strings with something like a == b, instead use a.equals(b). See "How do I compare strings in Java" for further details.
You might also want to look into the for-each loop if all you want to do is iterate over the collection. Your code can be rewritten as:
for (InstallationComponentSetup component : components)
if (component.getName().equals("ACQ"))
return true;
If you are comparing String , use equals() method .
Even your casting is wrong.You have to invoke next() on the iterator to get the next element . Hence it.next() gives you the next element which will be an object of InstallationComponentSetup, it is not of type InstallationComponentSetup hence the cast will fail.
Here you are casting the Iterator to your class type which will fail.
if (((InstallationComponentSetup)it).getName() == "ACQ")
{
return true;
}
I believe there is no need of cast here as you have defined the Collection to hold the specific type of element and also if you declare the Iterator of a specific type.
You can simply do :
// define Iterator of InstallationComponentSetup
Iterator<InstallationComponentSetup> it = components.iterator();
if("ACQ".equals(it.next().getName())) {
return true;
}
You can also consider using the enhanced for loop in Java , if your purpose is only to read the elements .
for(InstallationComponentSetup component: components) {
if("ACQ".equals(component.getName())) {
return true;
}
}
You have to retrieve the next element in the iteration before you compare:
InstallationComponentSetup next = it.next();
if (next.getName() == "ACQ")
{
return true;
}
Try to use the following code. It is more concise and easier to understand.
Collection<InstallationComponentSetup> components= context.getInstallationComponents();
for(InstallationComponentSetup comp : components){
if("ACQ".equals(comp.getName()){
return;
}
}
I think you had two problems in you code.
Cast the iterator to an object doesn't work like that. You need to use it.next() to get the object and move the iterator.
like already mentioned you need equals to compare Strings. == compares "memory locations" (in C++ terms).
Use it.next() to get the next element.
Also, use the .equals() method to compare strings in Java. Otherwise, the references are compared.
Finally, the cast should be unnecessary with a type-parameterized Iterator.
while (it.hasNext())
{
if ( it.next().getName().equals("ACQ") ) {
...
}
}
You have to retrieve the next element in the iteration before you compare:
java.util.Collection<InstallationComponentSetup> components= context.getInstallationComponents();
Iterator<InstallationComponentSetup> it = components.iterator();
while (it.hasNext()) {
if ("ACQ".equals(it.next().getName())) {
return true;
}
}
It would be easier to use foreach loop, make use of generic type, use equals for String and change string comparison order to be null secure.
Collection<InstallationComponentSetup> components= context.getInstallationComponents();
for (InstallationComponentSetup setup : components)
{
if ("ACQ".equals(setup.getName()))
{
return true;
}
}
The install4j API is still for Java 1.4, so there are no generics yet. This will work:
for (Object o : context.getInstallationComponents()) {
InstallationComponentSetup component = (InstallationComponentSetup)o;
if (component.getName().equals("ACQ")) {
return true;
}
}