question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question - Is it safe to remove(...) entries while iterating over an OakMap

See original GitHub issue

Java’s collections all don’t allow modifying one while iterating it, resulting in a ConcurrentModificationException. If I want to remove some entries from an OakMap while iterating through them, what is the proper way of achieving it?

For instance, I created a head map, tried to call clear() on it, and failed with UnsupportedOperationException. Iterator.remove is not implemented for those returned from an OakMap. And glancing the source code, I’m not quite sure if I can safely invoke remove on an OakMap, while iterating over its content.

Currently I’m iterating over the entries and calling remove immediately after I’m done with each entry. Is it ok to do so?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sanastascommented, Aug 15, 2021

@dynaxis , sounds great!

If you can explain a bit more your use-case, it will help us to understand our users more. Not urgent, when you have time. And if you want to know more about Oak internals, let me know. Thanks!

0reactions
sanastascommented, Aug 19, 2021

@dynaxis thank you so much for this detailed explanation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it safe to delete HashMap's entry when iterating over its ...
I have found a related question (iterating over and removing from a map), but it assumes we are iterating over the key set....
Read more >
Remove an Entry using key from HashMap while Iterating over it
Iterate over the HashMap using the Iterator.hasNext() method. While iterating, check for the key at that iteration to be equal to the key...
Read more >
How to Remove Entry (key/value) from HashMap in Java while ...
Can you remove a key while iterating over HashMap in Java? This is one of the interesting interview questions as well as a...
Read more >
for...of - JavaScript | MDN - MDN Web Docs
Each iteration executes statements that may refer to the current sequence value. When a for...of loop iterates over an iterable, it first calls ......
Read more >
Javarevisited: How to delete a key value pair from a HashMap ...
Yes, we'll iterate over Map to check each value but we'll not use the two remove() methods from ... How to delete an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found