简体   繁体   中英

Concurrent Modification Exception in Java Set

As a part of my program I stuck to Concurrent Modification Exception. Here is the mentioned part:

PriorityQueue<Customer> marginalGainHeap = new PriorityQueue<Customer>(
            1, new Comparator<Customer>() {
                public int compare(Customer c1, Customer c2) {
                    return Double.compare(c1.getMarginalGain(),
                            c2.getMarginalGain());
                }
            });

    // set of all remains available nodes
    availableNodes.removeAll(churnNet);
    for (Customer avail : availableNodes) {
        avail.setMarginalGain(0);
        marginalGainHeap.add(avail);
    }
    while (seedSet.size() <= budget) {
        **for (Customer remainingNode : availableNodes) {**

            remainingNode.setMarginalGain(calculateMarginalGain(
                    remainingNode, seedSet, network, availableNodes,
                    churnNet));

            marginalGainHeap.remove(remainingNode);
            marginalGainHeap.add(remainingNode);
        }
        seedSet.add(marginalGainHeap.poll());
    }

Here is the calculateMarginalGain Method:

private int calculateMarginalGain(Customer remainingNode,
            HashSet<Customer> seedSet,
            DirectedSparseGraph<Customer, Transaction> net,
            Set<Customer> availableNodes, HashSet<Customer> churnNetwork) {
        // Marginal gain for short-term campaign
        HashSet<Customer> tmp = new HashSet<Customer>(); // seedset U
                                                            // {remainingNode}
        tmp.add(remainingNode);
        Set<Customer> tmpAvailableNodes = availableNodes;
        HashSet<Customer> NeighborOfChurn = getNeighbors(churnNetwork, net);
        // sigma function for calculating the expected number of influenced
        // customers- seedSettmp=seedset U {u}
        tmpAvailableNodes.removeAll(NeighborOfChurn);
        Set<Customer> influencedNet = getNeighbors(tmp, net);
        tmpAvailableNodes.retainAll(influencedNet);
        return tmpAvailableNodes.size();
    }

I got this exception on the line of program that I specify with **. I find out that this error could be caused by Iterator. But I did not use any one! Please help me find out what caused that exception and how can I fix that?

Regards.

Converting you set to an array may solve your problem

Example:

Set<String> set = new Set<String>(size);
String[] array = set.toArray(new String[set.size()]);

So in your for loop you can do something like that:

for(String foo : set.toArray(new String[notis.size()])) {
    // Loop stuff here
}
**for (Customer avail : availableNodes) {**
    avail.setMarginalGain(0);
    marginalGainHeap.add(avail);
}

This is an iteration. It's called "simplified for loop". And you can't modify the elements you are iterating over when using this form of loop. Instead, use

for (int i = 0; i < availableNodes.size(); i++) {
 Customer currentNode = availableNodes.get(i);
 currentNode.setMarginalGain(0);
 marginalGainHeap.add(currentNode);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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