G
G
gibsonen2018-05-31 08:32:45
Java
gibsonen, 2018-05-31 08:32:45

ConcurrentModificationException, Hashmap and Leaf?

Hello throws Exception ConcurrentModificationException on this line for (Transaction t : c.getTransactions()) (line 64) for 2 iterations. I understand the very essence of the error - it happens when the elements of the iterator are deleted in the cycle. Tried to change remove to removeIF , but same error.

private static void iteration() {
        Integer n = 1;
        Integer k = 1;
        Double maxProfit;
        Integer clusterInd;
        while (k > 0) {
            System.out.println("Итерация " + n);
            n++;
            k = 0;
            for (int i=0;i<clusters.size();i++) {
                Cluster c = clusters.get(i);
                for (Transaction t : c.getTransactions()) {//ошибку кидает здесь на 2 итерации
                    maxProfit = profit();
                    clusterInd = -1;
                    c.deleteTransaction(t);
                    int j = 0;
                    for (Cluster cl : clusters) {
                        if (j != i) {
                            cl.addTransaction(t);
                            Double p = profit();
                            if (p > maxProfit) {
                                maxProfit = p;
                                clusterInd = j;
                            }
                            cl.deleteTransaction(t);
                        }
                        j++;
                    }
                    if (clusterInd == -1){
                        clusters.get(i).addTransaction(t);
                    }else {
                        k++;
                        clusters.get(clusterInd).addTransaction(t);
                    }
                }
            }
        }
        System.out.println(k);
    }

The function in which the transaction is deleted:
public void deleteTransaction(Transaction m) {
        if (this.count > 0) {
            String[] trans = m.getTrans();
            for (String s : trans) {
                this.square--;
                if (freq.containsKey(s)) {
                    if (freq.get(s) > 0) {
                        this.freq.put(s, freq.get(s) - 1);
                        if (this.freq.get(s) == 0) {
                            this.width--;
                            freq.entrySet().removeIf(entry -> entry.getKey().equals(s));
                        }
                    }
                }
            }
            this.count--;
            if (this.count > 0) {
                this.height = (double) this.square / this.width;
            } else {
                this.height = 0.0;
            }
            transactions.removeIf(m::equals);
        }
    }

Logs:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:939)
    at java.base/java.util.ArrayList$Itr.next(ArrayList.java:893)
    at com.lab6.ClopeAlgorithm.iteration(ClopeAlgorithm.java:64)
    at com.lab6.ClopeAlgorithm.main(ClopeAlgorithm.java:96)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-05-31
@gibsonen

Try to copy the c.getTransactions() collection

for (int i=0;i<clusters.size();i++) {
    Cluster c = clusters.get(i);
    List<Transaction> transactions = new ArrayList<>(c.getTransactions());
    for (Transaction t : transactions) {
        ...
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question