Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    Quote Originally Posted by JosAH View Post
    Show us your updated code for the filterXXX( ... ) methods.

    kind regards,

    Jos
    Filter methods:
    Java Code:
    	public void filterYear(Range r) {
    		for (Song song1 : songs2) {
    			if (song1.year > (r.getMax()) || (song1.year) < (r.getMin()))
    				itemsToRemove.add(song1);
    		}
    		songs2.removeAll(itemsToRemove);
    	}
    
    	public void filterRank(Range r) {
    		for (Song song1 : songs2) {
    			if (song1.rank > (r.getMax()) || (song1.rank) < (r.getMin()))
    				itemsToRemove.add(song1);
    		}
    		songs2.removeAll(itemsToRemove);
    	}
    
    	public void filterArtist(String s) {
    		for (Song song1 : songs2) {
    			if (!(((song1.artist).equals(s)))) {
    				itemsToRemove.add(song1);
    			}
    			songs2.removeAll(itemsToRemove);
    		}
    	}
    
    	public void filterTitle(String s) {
    		for (Song song1 : songs2) {
    			if (!(((song1.title).equals(s)))) {
    				itemsToRemove.add(song1);
    			}
    			songs2.removeAll(itemsToRemove);
    		}
    	}

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    Those methods look sensible to me; which one of them still gives you that exception? (did you clear the itemsToRemove collection before the methods are called?)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #23
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    The filterTitle and the filterArtist.
    I didn't clear the collection, that's a good idea if I want to run multiple commands. But both of these methods do not work even when they are run by themselves without other commands.

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    Hm, I can't see anything wrong with those methods; what happens when you try the 'good old' Iterator approach? I runs like this:

    Java Code:
    for (Iterator<Song> i= songs2.iterator(); i.hasNext(); ) {
       Song song1= i.next();
       if (song1 passes a test)
          i.remove();
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    The Iterator works! Thank you!
    I only wish I knew why my other methods wouldn't work.

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    Quote Originally Posted by Propinquity View Post
    The Iterator works! Thank you!
    I only wish I knew why my other methods wouldn't work.
    If you (structually) modify the collection in two separate threads or when an (other) Iterator is running over it, that exception is thrown; possbly you have another iterator running over that collection (somewhere else in the code). We have to see all of the (relevant) code to tell ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #27
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    Despite having a for-each loop that works, I'm still getting a ConcurrentModificationException. This is happening for the filterArtist and filterTitle methods only. filterRank and filterString both work.
    Java Code:
    public void filterArtist(String s) {
        for (Song song1 : songs2) {
            if (!(((song1.artist).equals(s)))) {
                itemsToRemove.add(song1);
            }
            songs2.removeAll(itemsToRemove);
        }
    }
     
    public void filterTitle(String s) {
        for (Song song1 : songs2) {
            if (!(((song1.title).equals(s)))) {
                itemsToRemove.add(song1);
            }
            songs2.removeAll(itemsToRemove);
        }
    }
    Notice that for both methods the songs2.removeAll(itemsToRemove) call occurs within the for loop?

    Going back to the quote from ArrayList (Java Platform SE 7 ),

    The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
    the removeAll call structurally modifies the list, and this is done not by using the iterator's add/remove methods, hence ConcurrentModificationException. In this case, this exception does not indicate that the list is being modified by separate threads - it can be thrown even when modification is done by the same thread.

    The following is a simple program to further illustrate this point:

    Java Code:
    import java.util.*;
    
    public class ListExceptionDemo {
    
        ArrayList<String> list;
    
        public ListExceptionDemo() {
            list = new ArrayList<>();
            list.add("aa");
            list.add("bb");
            list.add("cc");
        }
    
        public void filterList(String s) {
            ArrayList<String> itemsToRemove = new ArrayList<>();
    
            for (String item : list) {
                if (!item.equals(s)) {
                    itemsToRemove.add(item);
                }
                list.removeAll(itemsToRemove);
            }
        }
    
        public static void main(String[] args) {
            ListExceptionDemo led = new ListExceptionDemo();
            led.filterList("bb");
            System.out.println(led.list);
        }
    }
    Running this will give you

    Java Code:
    Exception in thread "main" java.util.ConcurrentModificationException
    	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    	at java.util.ArrayList$Itr.next(ArrayList.java:791)
    	at javaForums.ListExceptionDemo.filterList(ListExceptionDemo.java:19)
    	at javaForums.ListExceptionDemo.main(ListExceptionDemo.java:29)
    Not only that, it is also logically incorrect because the intention is not to call list.removeAll every iteration in the loop. It is fixed by moving list.removeAll out of the loop as follows:

    Java Code:
    	public void filterList(String s) {
    		ArrayList<String> itemsToRemove = new ArrayList<>();
    
    		for (String item : list) {
    			if (!item.equals(s)) {
    				itemsToRemove.add(item);
    			}
    		}
    		list.removeAll(itemsToRemove);
    	}
    or by using the Iterator approach as Jos recommended.

    Hth!

  8. #28
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    Quote Originally Posted by jashburn View Post
    Not only that, it is also logically incorrect because the intention is not to call list.removeAll every iteration in the loop.
    I haven't even noticed that, but it is Sunday, so I'm excused ;-)

    kind regards,

    Jos (<--- has just put the Grolsch in the fridge)
    cenosillicaphobia: the fear for an empty beer glass

  9. #29
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?


  10. #30
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main" java.util.ConcurrentModificationException?

    Quote Originally Posted by jashburn View Post
    Careful with those glasses; you don'y want to break them ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 11-03-2012, 12:38 AM
  2. Replies: 11
    Last Post: 09-04-2012, 05:33 AM
  3. Replies: 4
    Last Post: 05-07-2010, 08:58 PM
  4. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 08:29 AM
  5. Replies: 5
    Last Post: 05-14-2008, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •