7

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

4

8 回答 8

20

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 ...
}
于 2013-08-05T12:46:44.583 回答
2

If you use Eclipse Collections (formerly GS Collections) and change dbList to a MutableList or something similar, you can write:

MutableList<Long> dbList = ...;
RichIterable<RichIterable<Long>> chunks = dbList.chunk(10);

If you can't change the return type of dbList, you can wrap it in a ListAdapter.

RichIterable<RichIterable<Long>> chunks = ListAdapter.adapt(dbList).chunk(10);

Note: I am a committer for Eclipse Collections.

于 2013-08-06T19:03:09.533 回答
1

You can use the subList method in the List interface to divide your list.

于 2013-08-05T12:46:24.793 回答
0

In pure Java code it could be probably done:

int parts = list.size()/10;

        for(int i=0; i<10; i++){
            for(int j=0; j<parts; j++){
                Long l = list.get(j + i*j);
                // l do sth;
            }
        }
于 2013-08-05T12:47:20.487 回答
0

As d'alar'cop stated, I think it may be an issue with your SQL statement, rather than the list. But anyway, I would do something like this:

int i = 1;
List<Long> newList = new ArrayList<Long>();
for(Long item : dbList)
    {
        for (i <= 10; i++)
        {
            newList.Add(item)

            if (i == 10)
            {
                //SQL Statement
                i = 1;
            }

            break;
        }

    }

Or, a subList as the others have mentioned would work as well, and may be a more compact solution.

于 2013-08-05T12:50:51.853 回答
0

I don't know exactly what are you trying to do, but you could try something like the following pseudocode, if you build your query with a loop anyway:

counter=0;
for (Long item : dbList){
    query.add (item);
    if (counter>10){
        query.send();
        counter=0;
    }
}
if (!query.empty())
    query.send();

This is just pseudocode. Probably you don't have a query object like this, but is just to explain my point.

Hope it helps

于 2013-08-05T12:51:18.440 回答
0

Simple Class which contains function to return 10 elements from list which having a start index.

import java.util.ArrayList;
import java.util.List;

public class TestTenElement {

public static void main(String[] args) {
    // Getting all the element list from DB -- Here Creating Sample List
    List<Long> myList = new ArrayList<Long>();

    // Dummy element list
    for (long i = 0; i < 51; i++) {
        myList.add(i);
    }

    // Calling function may be from different class
    System.out.println(getFiveElement(myList, 0)); // First Ten Element from StartIndex 0
    System.out.println(getFiveElement(myList, 10)); // Second Ten Element from StartIndex 5
    // .......
    System.out.println(getFiveElement(myList, 50)); // If Last Iteration Gives remaining elements
}

// print ten element from list from the start index specified
public static List<Long> getFiveElement(List<Long> myList, int startIndex) {
    List<Long> sub = new ArrayList<Long>();

    sub = myList.subList(startIndex, Math.min(myList.size(), startIndex + 10));
    return sub;

}
}
于 2017-06-23T08:17:12.063 回答
0

Below code snippet might be helpful.

     int pointer =0;
     List<Long> chunks = null;
     List<Long> longValues =    LongStream.rangeClosed(1, 100).boxed().collect(Collectors.toList());
     int limit =10;
     do{ 
             chunks = new ArrayList<>();
             for(int incrementor =pointer ; incrementor < longValues.size() && incrementor < pointer+limit; incrementor++){
                 chunks.add(longValues.get(incrementor));
             }
             if(chunks.size() == limit){
                 pointer = pointer + limit;
             }

             if(chunks.size() >0){
                 System.out.println(chunks);
             //your update query goes here
             }
     }while(chunks.size() ==limit);
于 2019-12-03T06:50:49.140 回答