java - Creating new instance of object in loop to add in list -
i trying add object (javabean) list.
mywebservicerequest mywebservice = new mywebservicerequest(); myrequesttype type= new myrequesttype (); for(int i=0; i< 9; i++){ type.seta(somedynamicvalue); type.setb(somedynamicvalue); mywebservice.add(type); }
this creates 1 object of (type), add same object 9 times same data.
mywebservicerequest mywebservice = new mywebservicerequest(); for(int i=0; i< 9; i++){ myrequesttype type= new myrequesttype (); type.seta(somedynamicvalue); type.setb(somedynamicvalue); mywebservice.add(type); }
this creates multiple objects, add 9 diff object diff values .
what if (for loop) create hundreds of objects instead of 9 in single loop , each , every time request made? dump in memory right?
how avoid this?
thanks in advance.
when this
mywebservicerequest mywebservice = new mywebservicerequest(); myrequesttype type= new myrequesttype (); for(int i=0; i< 9; i++){ type.seta(1); type.setb(2); mywebservice.add(type); }
the line mywebservice.add(type);
adds object list , no matter state changed or not or same object. because list , not map not allow duplicates.
so count goes 10 , when
mywebservicerequest mywebservice = new mywebservicerequest(); for(int i=0; i< 9; i++){ myrequesttype type= new myrequesttype (); type.seta(1); type.setb(2); mywebservice.add(type); }
the code makes new object , add list , every object different . difference in 2 snippets , objects in second snippet not same , in first snippet same added 10 times.
Comments
Post a Comment