Hello - I have a situation where in I need to send a hashmap as an argument to another method. And I do not have any control over this other method and I have to make sure that the HashMap I am passing should not be updated.
I could have used Collections.unModifiableMap() to a get an unmodifiableMap but, the I need to specifically pass HashMap and not just any Map.
Please let me know how to achieve this.
Hi - thanks! and yes I have read the docs :), your one line just did not make sense to me as I was actually looking something where I dont need to create a copy and preserve the original.
Something similar to Collections.unModifiableMap().
I could even try extending HashMap, but thats too much to do...
Anyway Thanks again!
Why don't you want to create a copy?
Why do you care that the immutable map is specifically a HashMap? Normally that kind of tight coupling is indicative of a design flaw.
And, if for whatever bizarre reason, you really really can't create a copy and really really do need a HashMap, since you already knew from reading the docs before you posted that unmodifiableMap() doesn't give you what you need, why are you opposed to spending 5 minutes extending HashMap? How can that possibly be "too much to do"? There can't be more than half a dozen methods to override, and the overrides will each be one single line--you can even make them identical, so you'd be able to just copy and paste for all but the first.