title: Exception in thread "main" java.util.ConcurrentModificationException
body: <p>When I run the below code, I get an exception. I searched but couldn't find any solution.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Line number 45 is <em>for(String linkFromCollection : linksList){</em></p>
  
  <pre><code>public class Grabber {
  
  static String url;
  Document doc;
  static Set&lt;String&gt; linksList = new HashSet&lt;String&gt;();
  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: &lt;%s&gt;  (%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 &gt;= 100){
                  isCrawling = false;
                  System.out.println("STOP_WATCH IS " + STOP_WATCH);
              }
          }
  
  
      }
      ICVSWrite writer = new ICVSWrite();
  
      String[] strArray = (String[]) linksList.toArray();
      writer.write(strArray);
  
  }
  
  }
  </code></pre>