以下のコードを実行すると、例外が発生します。検索しましたが、解決策が見つかりませんでした。
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)
行番号 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);
}
}