Exception in thread "main" java.util.ConcurrentModificationException

When I run the code below, I get an exception. I searched, but could not find a solution.

Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$KeyIterator.next(Unknown Source) at com.aybits.software.linkgrabber.Grabber.main(Grabber.java:45) 

Line number 45 for (String linkFromCollection: linksList) {

 public class Grabber { static String url; Document doc; static Set<String> linksList = new HashSet<String>(); String matchingString ="java2s.com/Code"; static boolean isCrawling = true; static int STOP_WATCH = 0; public Grabber(String url){ Grabber.url = url; } public void grabLinks(String urlToCrawl) throws IOException{ doc = Jsoup.connect(urlToCrawl).timeout(20 * 1000).get(); Elements links = doc.select("a[href]"); for (Element link : links) { //print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35)); if(link.attr("abs:href").toString().contains(matchingString)){ if(!linksList.contains(link.attr("abs:href").toString())){ System.out.println("Added - " + link.attr("abs:href")); linksList.add(link.attr("abs:href").toString()); } } } } public static void main(String[] args) throws IOException { Grabber app = new Grabber("http://java2s.com"); app.grabLinks(url); while(isCrawling){ for(String linkFromCollection : linksList){ app.grabLinks(linkFromCollection); if(linksList.contains(linkFromCollection)){ STOP_WATCH += 5; System.out.println("STOP_WATCH IS " + STOP_WATCH); }else{ STOP_WATCH -= 1; System.out.println("STOP_WATCH IS " + STOP_WATCH); } if(STOP_WATCH >= 100){ isCrawling = false; System.out.println("STOP_WATCH IS " + STOP_WATCH); } } } ICVSWrite writer = new ICVSWrite(); String[] strArray = (String[]) linksList.toArray(); writer.write(strArray); } } 
+6
source share
2 answers

Line

 linksList.add(link.attr("abs:href").toString()); 

modifies the linksList collection during iteration over it. The next time through the for loop in main , Java calls next in the collection, sees that the collection has been modified and throws an exception.

When you execute the extended for loop, you cannot add or remove from the collection.

+17
source

You cannot call add on Collection while it goes around it. Here:

 for (Element link : links) { if(...){ if(...){ ... linksList.add(link.attr("abs:href").toString()); ^^^ <- here } } } 

You call the grabLinks method from your main method from a loop through linksList :

 for(String linkFromCollection : linksList) { app.grabLinks(linkFromCollection); 

You must add your elements to another Collection , and then copy them after.

Which puzzled me a bit why the exception came from the HashMap , as I assumed that the linksList was a List - obviously this is a Set . Not the best name in the world.

That should help .

+3
source

Source: https://habr.com/ru/post/944780/


All Articles