java


pojo declaraion outside forloop


I have an one scenario in my application,
I have to add 20 POJO objects into ArrayList. In that case I have a method like below.
public void methodname() {
ArrayList arr = new ArrayList();
for(int i=0;i<20;i++) {
User userobj = new User();
userobj.setName("xxx");
userobj.setFirstName("xxx");
userobj.setLastName("yyy");
arr.add(userobj);
} // end of for loop
}
one of friends suggest to change the UserObj declaration outside for loop.
public void methodname() {
User userobj = new User();
ArrayList arr = new ArrayList();
for(int i=0;i<20;i++) {
userobj.setName("xxx");
userobj.setFirstName("xxx");
userobj.setLastName("yyy");
arr.add(userobj);
} // end of for loop
}
in the first approach, I created the userobj inside the for loop, so when next iteration comes previous userobj will be eligible for garbage collector..
I would like to know is there any significant performance will improve on the second approach?
I would like to know is there any significant performance will improve on the second approach?
Did you experience a difference?
When programming you should focus on correctness and readability of your code.
Do not even Think about performance unless you have an actual performance problem and you have proven by profiling that a certain construct is the cause.
The ultimate universal performance tip is:
The fastest way to do something is not doing it.
So focus on good algorithms that avoid unneeded/hidden loops rather than on syntax variants that may improve performance.
The main difference between the first and the second approach is that in the first case you create 20 different User in memory which are assigned to the array.
In the second case you create only one User in memory but change its properties 20 times and assign the same User to all 20 positions of the array.
Regardless of the first scenario or the second, your User instances created are eligible for garbage collection only when the array itself is eligible for garbage collection, expect you remove the association with the array.
Garbage collection takes place only for an instance if there is no other instance referencing it.
Both the ways, will produce different results.
In the First one, you create 20 separate Objects, and add them to the List. Thus, changes on one of them, won't affect the others.
The Second, however, has the same object. Thus, changes on one will reflect the others. Actually, at the end, the attributes of the all the Objects would be the same, i.e, equal to the last added Object
Performance-wise, you can't say. The Objects would only be eligible for garbage collection, when there are no references pointing to them. However, as both the ways perform separate functions, you can't compare them.
Alas, your Friend is wrong. The First way is what you're looking for.
It needs to be inside the loop, otherwise you are only adding the SAME user 20 times !
On the second approach you will have only one last object in the list
I think that the right way is:
- declaration outside
- instantiation inside
public void methodname() {
ArrayList arr = new ArrayList();
User userobj;
for(int i=0;i<20;i++) {
userobj = new User();
userobj.setName("xxx");
userobj.setFirstName("xxx");
userobj.setLastName("yyy");
arr.add(userobj);
} // end of for loop
}

Related Links

How gradle build a android project from source code to apk
Add date from JXDatePicker into SQL Database in Netbeans
Apache FOP change default cache-file folder
How to handle error in generateState in SyncOnSubscribe RxJava?
Unable To Access My Widgets In Android Studio
org.hibernate.MappingException: Could not determine type for: at table: for columns: [org.hibernate.mapping.Column(plant)
To update my layout elements continuously in an event method from inside the event method using loops
Adding MouseListener breaks mouse event dispatching
How can I enable project tango in Normal camera mode and take 3D depth data?
Why I need to find an object with exactly the same lifecycle with the listerner object when using a weak reference?
How to manage messages in apache ActiveMQ
ScrolledComposite does not scroll when focus is inside Text (Mac only)
where are my source files in IntelliJ project?
Error message in Java [duplicate]
Android App Emulator Issue
Add element to synchronizedList if not exists

Categories

HOME
azure-data-factory
kde
lodash
iot
xmpp
baqend
fancybox
onelogin
constraint-programming
bs4
amazon-cloudformation
usergrid
dynamics-crm-online
serverless-framework
workload-scheduler
windows-7-x64
samoa
reverse-proxy
excel-2007
jndi
semantic-analysis
url-scheme
neo4j-spatial
unboundid
bcd
noraui
catch-all
subset-sum
openoffice.org
xmlreader
html5-fullscreen
catalog
bower-install
objectlistview
uft-api
ws-security
particles.js
unoconv
core-plot
google-closure
jvm-languages
estimote
flashair
errordocument
pnotify
alphabet
hls.js
gabor-filter
nstextview
elgg
nativeapplication
kbuild
sqlclient
django-scheduler
angular-strap
impresspages
ubuntu-10.04
bgp
fadeout
suffix-tree
netmq
hsv
msys
sdhc
0xdbe
cakephp-3.1
dundas
key-management
npapi
ng-animate
qcodo
html-helper
aapt
navigationservice
xceed-datagrid
poller
mbr
excel-2003
jplaton
pushbackinputstream
clipper
word-processor
datawindow
jspinclude
appender
recent-documents
nsviewanimation
bespin
fixed-width
web-architecture
temporal-database
django-notification

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App