how to retrieve the data and set into a list upto list size 10

Hi Experts,

Can some help me with code logic

I have to fetch some data from db and i will convert object []  into list.

if listsize is less than 10 (list.size()<10)

i have to execute another query to fetch the data from db and convert this data into list

finally i have to set this list data into old list

Example:
1.from first query if i fetch data upto 5reocords  then
2.rest of the records i will fetch it from second query
3.finally i have to set all 10 records into one list

Can some one help me with sample code

Thanks,
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Mark BullockQA EngineerCommented:
If you're asking how to append one list to another, you would do it like this:
if (list.size() < 10) {
   list2 = //fetch more records
   //append the second list to the first
   list.addAll(list2);
}

Open in new window

srikoteshAuthor Commented:
HI

FROM first query i may get 10 records some time.so that time i will not use second query.
if first query fetch only 6 records
i have to fetch rest of records from second query but second query will give n no of records.
i have to get only 4 records i dont want rest of the records from second query.
Mark BullockQA EngineerCommented:
Add as many records as you need from the second query
int recordsToAdd = 10 - list.size();
if (recordsToAdd > 0) {
   list2 = //fetch more records
   //append the second list to the first
   for (Object element : list2) {
      list.addAll(element);
      recordsToAdd--;
      if (recordsToadd == 0) {
         break;
      }
}

Open in new window

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

srikoteshAuthor Commented:
With this approach there is performance issue

while fetching second query
let suppose second query fectch 100 records
we are taking only less than 10 records
as per above as u mentioned.

i want to set limit at dao layer itself while hitting the DB.
I have a common method in dao layer with pagenation limit with 10 records
i will pass recordsToAdd value to that method and i will limit here itselft w/o fetching all the records from DB.
Please find the attachment.

Example:
recordsToAdd  = 17
first time it will fetch 10 records and  then 7 records
DAO.TXT

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
srikoteshAuthor Commented:
is it possible to set limit in the above attached method

or

i have to write a query with limit
Mark BullockQA EngineerCommented:
setMaxResults should work.
srikoteshAuthor Commented:
thanks
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.