2

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.

1
  • Such exception arise if another thread trys to update the collection while iterating it.Plz check Commented Mar 10, 2014 at 19:04

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

1
**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);
}

13 Comments

But you are iterating availableNodes and updating marginalGainHeap, so wht is the issue ??
The "setMarginalGain" on the current node represents a modification of that node.
I dnt find the implementation of this method , where u found??
It's probably in some part of the code that the OP did not post here.
Its not proper way to post any answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.