• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections.unmodifiable* and Immutability

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is probably answered before, but can anyone give me a quick answer on this.

If I do a Collections.unmodifiableMap - the returned Map is immutable (read-only), but what about the elements inside the Map? If the Map itself was filled with a bunch of different objects such as HashTables, Arrays, etc, can I modify them? Or is this whole unmodifiable property a recursive one - that goes down all the object layers inside?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perry Terrance wrote:I know this is probably answered before, but can anyone give me a quick answer on this.

If I do a Collections.unmodifiableMap - the returned Map is immutable (read-only), but what about the elements inside the Map?



Both the keys and the values are precisely as mutable as they were before. The only thing unmodifiableXxx() does is prevent you from changing the structure of the collection--can't add, remove, rearrange.

It is a "shallow" immutability.
 
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perry Terrance wrote:If I do a Collections.unmodifiableMap - the returned Map is immutable (read-only), but what about the elements inside the Map? If the Map itself was filled with a bunch of different objects such as HashTables, Arrays, etc, can I modify them? Or is this whole unmodifiable property a recursive one - that goes down all the object layers inside?



It just makes the returned Map unmodifiable. That's all. If it did do something ambitious like having the get() methods return unmodifiable versions of the objects in the Map, you can be sure that the documentation would mention that. And it doesn't.
 
Perry Terrance
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

Just the thing I needed was this exact setup!

So "shallow" immutability is what I need since I need to tinker with the actual objects inside the Map...

On a side note, I wonder if there was ever a way to do this recursively though, if I ever wanted a full-recursive immutability...
 
Paul Clapham
Marshal
Posts: 28263
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perry Terrance wrote:On a side note, I wonder if there was ever a way to do this recursively though, if I ever wanted a full-recursive immutability...



The minimum requirement for that (I think) would be to be able to make an unmodifiable version of an arbitrary object. This would require returning a wrapper, or a proxy, or something like that. And that proxy would have to throw an exception when something called any method which would have modified the state of the original object.

And that's the hard part. How can you tell whether a method is going to modify the state of an object?

I'm not saying that couldn't be done. I just think it would take some heavy-duty techniques like (for example) the tricks Hibernate uses to tell when you've updated a Hibernate object.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perry Terrance wrote:
So "shallow" immutability is what I need since I need to tinker with the actual objects inside the Map...



Be careful though. If you change the contents of a key in a way that affects the results of hashCode(), the map may not be able to find that key. You should generally use only immutable objects for Map keys and Set entries, or else be very careful not to change their states after adding them.

On a side note, I wonder if there was ever a way to do this recursively though, if I ever wanted a full-recursive immutability...



To a limited extent, yes. You could create your own Map implementation (either extend, or better yet, delegate to) an existing implementation. You then have to tell each element to make itself immutable. This means you can only use classes you write (or that are already immutable) as elements.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perry Terrance wrote:I know this is probably answered before, but can anyone give me a quick answer on this.
If I do a Collections.unmodifiableMap - the returned Map is immutable (read-only), but what about the elements inside the Map? If the Map itself was filled with a bunch of different objects such as HashTables, Arrays, etc, can I modify them? Or is this whole unmodifiable property a recursive one - that goes down all the object layers inside?


The best analogy I can give you is that it's like being passed an array as a parameter. You can't change the array itself, but you can change one of the elements if the element type itself is mutable. In fact, it slightly more restrictive, because with an array you can replace an element; with an unmodifiable collection you can't.

HIH

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correction withdrawn, as Winston has gone back and clarified his post.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Sorry, but that analogy doesn't quite hold. If you're passed an array, you can change the value of one of the array's elements.


Blimey, you're quick! I was just correcting the post and got a 'caught us doing modificiations' message when I sent. See updated version now.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic