• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cloneable Interface  RSS feed

 
Karthikeyan Ramaswamy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, all

In my application, i am using session attributes like

1. session.setAttribute("PlanDetails",arrayList);

i updating this session on some condition,i maintained original list in some other session attribute like,

2.session.setAttribute("InitialPlanDetails",arrayList);

first one is will be modifiable, second one is initial values

here is a problem,

if i update session attribute "PlanDetails"
reflecting on "InitialPlanDetails" in that session also,
so i tried clone like below,
it's clone only this arraylist not cloning classes inside that arraylist

2.session.setAttribute("InitialPlanDetails",arrayList.clone());

how can i solve this problem,

thks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList.clone only does a shallow clone. If you want clones of all the objects in the list then you will need to create a new ArrayList, and then iterate through the old list creating clones of each of the objects which you then add to the new list.
 
Karthikeyan Ramaswamy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, Joanne

i tried that too, it's working but i have almost 5000 objects in that arrylist
so it take long time,

can tell me any other way without iterating arraylist.

thks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Karthikeyan Ramaswamy:

i tried that too, it's working but i have almost 5000 objects in that arrylist
so it take long time,

can tell me any other way without iterating arraylist.


It's not the iterating that's taking a long time; it's making copies of the 5000 objects. The only way to make it faster would be to make it smarter.

Here's one idea: instead of having a plan be an ArrayList of PlanItems, you could have a Plan class. Imagine that a Plan object A can have a pointer to another Plan object B. Imagine that A stores only changes to B, rather than a copy of the whole thing. Now creating the new plan B is a fast operation, because we don't need to copy all of A. Make sense?
 
Karthikeyan Ramaswamy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply, i will do that.
[ May 23, 2008: Message edited by: Karthikeyan Ramaswamy ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!