Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

return copy instead of referance to collection

Thread view: 
sakcee@gmail.com - 30 Apr 2007 22:47 GMT
is there a way to return a copy of collection instead of referance to
collection itself

I have a wrapper class to a collection('Big' HashMap) data structure.
I want to give an accessor method
but I want to  return a copy of the structure instead of refernace to
structure

should I  copy  key by key?, can i use clone? is there a way to give
final referance so that user can not
chnage it?

which is better ?

thanks
Daniel Pitts - 30 Apr 2007 22:52 GMT
On Apr 30, 2:47 pm, "sak...@gmail.com" <sak...@gmail.com> wrote:
> is there a way to return a copy of collection instead of referance to
> collection itself
[quoted text clipped - 11 lines]
>
> thanks

There are a few options.
Collections.unmodifiable* will give you a wrapper Map or Collection
which the client cannot modify.  It will be a view of the existing
collection, so that if your code changes the Map, the unmodifiable map
will change, but any calls to set/put in the unmodifiable map will
through unsupported exception.

Alternatively, you can usually
public Map<T> getMap() {
  return new HashMap<T>(oldMap);
}

Hope this helps.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.