0

Pls help me with this problem. I have Graph represent by Structure HashMap> graf; and i need to go throught that hashmap and set and remove some edges. How can i do that ? code give me this exception

Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
        at java.util.HashMap$KeyIterator.next(Unknown Source)


      public String vratCestu() {
            String vrat = "";
            for(String s: this.graf.keySet()){
                if (this.graf.get(s).size() != 0)

                    for(String k : this.graf.get(s)){
                            this.graf.get(s).remove(k);
                            this.graf.get(k).remove(s);
                            vrat += k + "; ";

                    }
                }
                return vrat;
            }
1

2 Answers 2

1

Instead of enhanced for loop for(String s: this.graf.keySet()), use an Iterator You cannot modify the collection while iterating over it with the use of enhanced for loop.

Sign up to request clarification or add additional context in comments.

2 Comments

i am beginner in java pls can u write me an exaple how can i do it ?
Refer the link that @peter-lawrey has suggested in his comment
0

You cannot modify your HashSet directly while iterating over.

Comments

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.