0

suppose you have a HashMap m and there is already a key value pair <"key1", object> inside.

can you do the following?

m.put("newkey", m.remove("key1"))

will you get a ConcurrentModificationException?

5
  • 4
    just try it before asking ;-) Commented Nov 28, 2011 at 14:37
  • Why is this being closed? I think the question is clear...? Commented Nov 28, 2011 at 14:37
  • @Matteo in the world of software programming, there is try it once or twice doesn't reveal the true answer, what you seen is a fragment of our code. problems somethimes can be difficult to reproduce, especially threading issue. even after try it, I still want to know why it worked, or why it did not work, personally I think give people option to close post , is very bad practice. becasue there are too many people here, way smart then the average Commented Nov 28, 2011 at 15:19
  • @shanyangqu First of all, I did not ask to close this post. Instead, I saw that the question was trivial, and there was a good answer to that (which I voted for). As you might see from my questions, I AM a software engineer, and I write programs. In my experience, I saw that software systems using a single thread without any I/O, tend to be deterministic. And the code you posted only uses a single thread and does not perform any I/O. So, it is likely to be deterministic. Hence I suggest to run it (alone, without the rest of your app), debug it, and understand how simple was your question. Commented Nov 28, 2011 at 18:34
  • @Matteo: you are of course right in this part: "And the code you posted only uses a single thread and does not perform any I/O. So, it is likely to be deterministic. ". But given the question, you wrongly assume that the asker would be in possession of this piece of information. Trying it would not reveal the reason why it is valid code anyway, or the hint that, for readability, you should use two lines instead of just one (with a temporary variable called "removedValue" for instance). Commented Nov 29, 2011 at 23:31

4 Answers 4

7

You can do that as long as it's not in the body of a loop that is iterating over the hashMap entries. The way that will work is that the remove operation will execute and complete before the put operation so it's semantically equivalent to doing it in 2 lines.

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

8 Comments

Because if the loop is iterating over the map you can't modify the underlying map without triggering the concurrentModificationException since you may be inserting a new row that shows up in the map before your current place in the iterator (for instance).
@shanyangqu You could do that in a loop unless you use an iterator over that hashmap (useing a foreach loop etc.). Those iterators check whether there were modifications not made through them and throw the ConcurrentModificationException.
@Thomas, I believe the Iterator way is the way to go if you intend to modify or remove entries from the Map. See stackoverflow.com/questions/1066589/…
Note: an Iterator for HashMap will trigger a concurrent modification exception even if you do remove or put in the same thread. If you use ConcurrentHashMap it will not produce a ConcurrentModificationException as its designed not to.
@Hamidam, but not if you want to change keys, which requires the use of a put().
|
1

Just tested it for you.

Map<String, Object> map = new HashMap<String, Object>();        
map.put("k1", Integer.valueOf(999));        
map.put("k2", map.remove("k1"));
System.out.println(map.get("k2"));

Prints:

999

No exception (ConcurrentModificationException).

Comments

0

Since m.remove returns the object previously associated with that key, you should be able to use that object however you like. So no, I don't believe you should get an exception.

Comments

0

They are actually not fired simultaneously. The remove is called first, and done with, then the get is called, so I see no reason as to why there would be an exception.

See this if you need to modify when looping: Iterate through a HashMap

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.