How i can iterate list to get 10 elements each time in java

java, list, mysql

Solution

`ArrayList#subList` is a very efficient operation. You can iterate over ranges of size 10:

for (int i = 0; i < dbList.size(); i += 10) {
   List<Long> sub = dbList.subList(i, Math.min(dbList.size(),i+10)));
   ... query ...
}

Problem

I have a list with 70 elements. For Example: `List<Long> dbList = new ArrayList<Long>();` `dbList` has 70 records. If I send all the records to a query in MySql it takes a long time. So now I want to send each time 10 elements to database query. So I need to iterate over the list in intervals of 10. How can I do this? Is this a good approach to avoid long time when using `IN`. SQL Query ``` select model.boothId, model.panchayat.panchayatId from Table1 model where model.panchayat.panchayatId in(:locationValues) and model.publicationDate.publicationDateId in (:publicationDateIdsList) and model.constituency.id = :id group by model.panchayat.panchayatId ``` Thanks in advance...

Original source