[英]Exception in thread “main” java.util.ConcurrentModificationException
When I run the below code, I get an exception. 当我运行以下代码时,我得到一个例外。 I searched but couldn't find any 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 is for(String linkFromCollection : linksList){ 第45行用于(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);
}
}
The line 这条线
linksList.add(link.attr("abs:href").toString());
modifies the linksList
collection while you are iterating over it. 在迭代它时修改
linksList
集合。 The next time through the for
loop in main
, Java calls next
on the collection, sees that the collection has been modified, and throws the exception. 下一次通过
main
循环中的for
循环,在集合上next
调用Java,看到集合已被修改,并抛出异常。
When you are doing an enhanced for
loop, you cannot add to or remove from the collection. 当您执行增强的
for
循环时,无法添加到集合或从集合中删除。
You cannot call add
on a Collection
while looping over it. 循环时,不能在
Collection
上调用add
。 Here: 这里:
for (Element link : links) {
if(...){
if(...){
...
linksList.add(link.attr("abs:href").toString());
^^^ <- here
}
}
}
You call grabLinks
method from your main
method from within a loop over linksList
: 你叫
grabLinks
从你的方法main
方法从环比内linksList
:
for(String linkFromCollection : linksList) {
app.grabLinks(linkFromCollection);
You have to add your items to another Collection
and then copy them over after. 您必须将您的项目添加到另一个
Collection
,然后将其复制。
What had me puzzled for a little while was why the exception was coming from a HashMap
as I had assumed that linksList
was a List
- obviously it's a Set
. 令我困惑的是,为什么异常来自
HashMap
因为我假设linksList
是一个List
- 显然它是一个Set
。 Not the best name in the world. 不是世界上最好的名字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.