-2

I have an ArrayList and I am iterating over the list to build a query, but my query can have only 20,000 characters so I am trying to iterate over certain index so I can have multiple queries. How can I do it? Any guidance would be helpful.

for(int i=0; i < EntryList.size(); i++){

    String entryValue = (String) EntryList.get(i);

    Query = Query + entryValue;
}

Say my list has 500 items, I want to iterate over every 100 or 200 items to get my query.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
JFed
  • 17
  • 4
  • 1
    https://stackoverflow.com/questions/12026885/common-util-to-break-a-list-into-batch – shmosel Nov 01 '18 at 20:30
  • Unrelated, but please learn the Java naming conventions and stick to them. For example, object references should begin with a lowercase and be camel-cased: `entryList` – Zephyr Nov 01 '18 at 20:30
  • 2
    This solution sound's weird. Do you wanna share your problem? Maybe theres another way to solve this. – cvdr Nov 01 '18 at 20:30

1 Answers1

0

You can do this by creating a separate List to hold each individual query that adheres to your length limit.

Then we just iterate over your list of entries and build each query. Once the query reaches your maximum length, add it to the queriesList and start over on building a new query.

Your question isn't incredibly clear on your requirements or the end goal, but here is a simple MCVE to demonstrate.

It will build queries up to 100 characters in length.

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

class Scratch {
    public static void main(String[] args) {

        // Create a sample list of entries, with 50 items
        List<String> entryList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            entryList.add("Entry" + i);
        }

        // Create a list to hold our list of queries that are all under the maximum length
        List<String> queriesList = new ArrayList<>();
        int maxQueryLength = 100;

        // Create a new StringBuilder to add our entries to
        StringBuilder tempQuery = new StringBuilder();

        // Loop through all entries and build a query, up to the max characters
        for (String entry : entryList) {

            // Check the length of the current query
            if (tempQuery.length() + entry.length() > maxQueryLength) {
                // Add the tempQuery to our list of queries and start fresh
                queriesList.add(tempQuery.toString());
                tempQuery.setLength(0);
            }

            tempQuery.append(entry);
        }

        // We now have our list of separate queries that we can run individually
        for (String query : queriesList) {
            System.out.println(query);
        }

    }
}

If, however, you do want to have a specific number of entries in each query (instead of number of characters per query), you can reset the StringBuilder after every 100-200 items:

    for (int i = 0; i < entryList.size(); i++) {

        // If this iteration is a multiple of 100, start a new query, otherwise just add to it
        if ((i + 1) % 100 == 0) {   // We add 1 to i here to avoid adding an empty query on the first pass
            // Add the tempQuery to our list of queries and start fresh
            queriesList.add(tempQuery.toString());
            tempQuery.setLength(0);
        }

        tempQuery.append(entryList.get(i));
    }
Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • Thank you for your reply, what I am trying to do is create a query for my target system to read the value of entryList, which will return results of entryList with their value, my entryList is more than 35000 and if I write a query for entire list it goes to more than the minimum characters my target system would accept so hence trying to create multiple queries for set of entryList (for example - select Value,EntryID from EntryTable where EntryID = '2030405060' or EntryID = '2030405061' or ..........), I can do it for each entryList but it will take a long time to run through 35000 – JFed Nov 01 '18 at 21:57
  • Both ways left the last items in the list. – JFed Nov 08 '18 at 22:44