how to remove iteration of a same field no of times inside for loop in java

Hi Experts,

.
List<Object> information = dao.getDetails()//getting the data from database

if(!information.isEmpty()){
List<information> list = new ArrayList<>();
for(Object object:information){
Object[] row = (Object[])object;
final InformattionDto info = new InformattionDto();
info.setName((String) row[0]);
info.setAddress((String) row[1]);
info.setInformationId((Long) row[2]);
list.add(info);
}
}
return list;

Open in new window



let suppose if i fetch 5 records from the database
5 records i am iterating and setting to the list

info.setInformationId((Long) row[2]);
the above line informatiionId will be the same for all records
is there any way to get the information w/o iterating no of items

can some one suggest me how to do it.

Thanks
LVL 2
srikoteshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
info.setInformationId((Long) row[2]);
the above line informatiionId will be the same for all records
is there any way to get the information w/o iterating no of items

I'm not sure what you're trying to avoid doing?

It seems like you need to call info.setInformationID() for each info object.
So you need to loop and call this set method once per info object, even if the value is the same for each one.

You could put the value from row[2] into a variable and re-use it, but that saves you nothing and just makes the code more complex.

So I think what you have is basically fine.

Doug
0
srikoteshAuthor Commented:
Hi dpearson,

Let suppose i have 2000 records
for all 2000 records,i have the same Information Id.
why i have to iterate this one those many times.if i skip this part performance will increase.
0
dpearsonCommented:
if i skip this part performance will increase.

It won't in any way you can measure, even for 100 million records.

The reason is that the line above it:
List<Object> information = dao.getDetails()//getting the data from database

where you actually go get the data from the database is an "order of magnitude" slower.

That means the call to look up the data from the database (which involves sending a query over a network to another computer, parsing a query, looking up the data from disk, collecting it into rows, packaging it so it can be sent back over the network, sending it over the network) will always take at least 10x (one order of magnitude) or more likely 100x or 1000x or 10000x slower (several orders of magnitude) than your loop to store the retrieved data into a list of objects.

This is a general lesson in optimizing code.  First find out where it's slow.  Only then, try to speed it up.

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.