0

My program creates an arraylist of 5000 to 60000 records depending on time of day. I want to split it into as many arraylists as possible that each arraylist will have 1000 records. I looked at many examples online and tried a few things but I ran into strange problems. Can you please show me an example of this?

Regards!

1
  • 1
    Where is the code that caused you problems? Commented Jul 16, 2012 at 1:24

2 Answers 2

2
  public static <T> Collection<Collection<T>> split(Collection<T> bigCollection, int maxBatchSize) {
    Collection<Collection<T>> result = new ArrayList<Collection<T>>();

    ArrayList<T> currentBatch = null;
    for (T t : bigCollection) {
      if (currentBatch == null) {
        currentBatch = new ArrayList<T>();
      } else if (currentBatch.size() >= maxBatchSize) {
        result.add(currentBatch);
        currentBatch = new ArrayList<T>();
      }

      currentBatch.add(t);
    }

    if (currentBatch != null) {
      result.add(currentBatch);
    }

    return result;
  }

Here's how we use it (assuming emails an a large ArrayList of email addresses:

Collection<Collection<String>> emailBatches = Helper.split(emails, 500);
    for (Collection<String> emailBatch : emailBatches) {
        sendEmails(emailBatch);
        // do something else...
        // and something else ...
    }
}

where emailBatch would iterate over the collection like this:

private static void sendEmails(Collection<String> emailBatch){
    for(String email: emailBatch){
        // send email code here.
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It looks like it works, but how would I extract data from a collection? I've never used collections before.
You have. :) The ArrayList is a collection. Most likely you'll want to iterate over the collection. I've added an example usage.
1

You can use the subList http://docs.oracle.com/javase/6/docs/api/java/util/List.html#subList from List to split your ArrayList. The sublist will give you a view of the original list. If you really want to create a new list, separate from the old one, you could do something like:

int index = 0;
int increment = 1000;
while ( index < bigList.size() ) {
   newLists.add(new ArrayList<Record>(bigList.subList(index,index+increment));
   index += increment;
}

Note you'll have to check for off by one errors here. This is just a quick pseudocode sample.

3 Comments

I think the OP wants 5-60 separate ArrayLists containing 1000 records each. Not 5-60 ArrayLists that provide different views of the same array of 5000-60000 records.
That code won't compile. I think you meant new ArrayList<>(bigList.subList(index, index+increment)).
Thanks for your answer, this is what I have now pastebin.com/hEpXikws but I get the following runtime error java.lang.IndexOutOfBoundsException: toIndex = 29000 at java.util.SubList.<init>(Unknown Source) at java.util.RandomAccessSubList.<init>(Unknown Source) at java.util.AbstractList.subList(Unknown Source) at proxymanager.Main.main(Main.java:68)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.