• 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

How to properly return read-only List?

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to return List member variable from my class. If I do return List contained in class then the caller will be able to modify it. I do not want anybody to be able to modify List. If I do return cloned List it's ok, but cloning is expensive. I saw people returning Iterator to list. That would be the best except for Iterator has remove() method. How to properly return a list that can be viewed but cannot be modified?

Vladas
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collections.unmodifiableList()
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but cloning is expensive.


Why are you so sure of that? Did you do timing trials? Seems to me that a shallow clone should not take any more time than creating a wrapper object. The clone method in java.lang.Object involves native code - presumably a pure copy operation.
Bill
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vladas,

Collections.unmodifiableList() is a good start, but not necessarily
good enough. Imagine that you need to return a list of lists. Then
unmodifiableList will only make the top-most list unmodifiable. Users
will still be able to get references to the "internal" lists and
change them.

Niether shallow copying helps you in this case. The only solution is
that all members of the List are unmodifiable too.

Best regards,
Petr
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
William, I am not sure about that and won't argue about cloning.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Petr,

Very good point. There is no solution about it. I have always to keep an eye what's contained inside the list.

Vladas
 
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just in case anyone is interested

org.apache.commons.collections.IteratorUtils.unmodifiableIterator()

allows you to wrap an read-only iterator.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic