how to retrieve object from java hashSet

I am looking for a way to retrieve object from hashSet in Java. I did iteration over its elements like this:

for (Customer remainingNode : availableNodes) {
                    remainingNode, seedSet, network, availableNodes,

Unfortunately due to concurrent modification Exception I have to change that to something like this:

for(int i=0;i<numberofRemainingNodes;i++){
Customer remainingNode=availableNodes.get(i);
                        remainingNode, seedSet, network, availableNodes,

But I can not do that because there is not any get(index) method for Java hashSet. Would you please help me to handle this situation?

P.S: I used HashSet because of I want to handle the union and intersection situation and I did not want to add duplicate element to that. Please consider that this part of my program should be run millions of times so a little extra latency could be expensive for whole program.


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}
        Set<Customer> tmpAvailableNodes = availableNodes;
        HashSet<Customer> NeighborOfChurn = getNeighbors(churnNetwork, net);
        // sigma function for calculating the expected number of influenced
        // customers- seedSettmp=seedset U {u}
        Set<Customer> influencedNet = getNeighbors(tmp, net);
        return tmpAvailableNodes.size();

private HashSet<Customer> getNeighbors(HashSet<Customer> churnNetwork,
        DirectedSparseGraph<Customer, Transaction> net) {
    HashSet<Customer> churnNeighbors = churnNetwork;
    Collection<Customer> neighbors = new HashSet<Customer>();
    for (Customer node : churnNetwork) {
        neighbors = net.getNeighbors(node);
        for (Customer neighbor : neighbors) {
    return churnNeighbors;


The problem in your code is that you change the structure of your HashSet during the iteration It is within the calculateMarginalGain() method, in this line:


Think twice whether this is really right! If yes, then you can work easily around the problem by making you a copy of the set for the iteration first. E.g.:

Set<Customer> copy = new HashSet<Customer>;
for (Customer : copy) {

Actually tmpAvailableNodes and availableNodes are the identical set. Maybe you can improve here in general.

You have to use an Iterator:

Iterator<Customer> custIter = availableNodes.iterator();
while(custIter.hasNext()) {
    Customer customer =;
    // do your work here

Using this you won't get ConcurrentModificationException. It is not clear why you get it though. If you are tampering with the HashSet from multiple Threads consider using a concurrent data structure instead.

If you modify availableNodes in setMarginalGain you will still get the exception though.

Need Your Help

Multiple responses in Stata


Let's say you have a survey dataset, with 12 variables that stem from the same question, and each variable reports a response option for that question (multiple-response options possible for this

Optimizing BeautifulSoup (Python) code

python optimization beautifulsoup

I have code that uses the BeautifulSoup library for parsing, but it is very slow. The code is written in such a way that threads cannot be used.