简体   繁体   English

HashSet中的并发修改异常

[英]Concurrent Modification Exception in HashSet

I'm getting the Concurrent Modification Exception error with this code: 我收到此代码的“并发修改异常”错误:

Iterator iterateBids = bidders.iterator();
     Person nextBidder;

bidders.add(bid.getBidder());

if (highestBid == null) {
    // There is no previous bid.
    highestBid = bid;
    while (iterateBids.hasNext()) {
        //nextBidder = (Person) iterateBids.next();
        ((Person) iterateBids.next()).bidUpdate(this, bid);
    }
    return true;
} else if (bid.getValue() > highestBid.getValue()) {
    // The bid is better than the previous one.
    highestBid = bid;
    while (iterateBids.hasNext()) {
        nextBidder = (Person) iterateBids.next();
        nextBidder.bidUpdate(this, bid);
    }
    return true;
}

bidders is a HashSet containing Person. bidders是一个包含Person的HashSet。 The error occurs at "nextBidder = (Person) iterateBids.next();" 错误发生在“ nextBidder =(Person)iterateBids.next();”处 The code looks for a bid higher than the current bid. 该代码查找的出价高于当前出价。 If higher, it iterates through the bidders set and performs the bidUpdate() method. 如果更高,则迭代投标者集并执行bidUpdate()方法。

How am I modifying and iterating at the same time? 我如何同时修改和迭代?

Only thing I can see is that you grab the iterator before this line: 我只能看到的是您在此行之前获取了迭代器:

 bidders.add( bid.getBidder() );

...which is clearly a modification to bidders . ...这显然是对bidders的修改。

After getting the iterator at line 1: 在第1行获得迭代器后:

Iterator iterateBids = bidders.iterator();

you are modifying the data structure at line 4: 您正在第4行修改数据结构:

bidders.add( bid.getBidder() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM