-1

I have a List of size 30K and I split them into chunks of 1000 as I need to use the ids from the sublist to get rest endpoint and do some assertions. I am using jersey client to talk to rest endpoints

ArrayList<String> ga1 = new ArrayList<>(Ids.subList(1,1000));
ArrayList<String> ga2 = new ArrayList<>(Ids.subList(1001,2000));
ArrayList<String> ga3 = new ArrayList<>(Ids.subList(2001,3000));
ArrayList<String> ga4 = new ArrayList<>(Ids.subList(3001,4000));
...
ArrayList<String> ga30 = new ArrayList<>(Ids.subList(29001,sizeOfTheOriginalList));

checkSubList(ga1);
{
    Assertions;
}

checkSubList(ga30);
{
    Assertions;
}

Is there a better way to split the original list and get the sublist

azro
  • 53,056
  • 7
  • 34
  • 70
user0007
  • 39
  • 5

1 Answers1

0

The principle is called "Don't repeat yourself." Your lines of code have lots of repetitions (for ga1, ga2, ec.

Other hints: - Don't declare the varible as ArrayList, declare it as the interface that you care about, List. - The variable Ids should be ids according to Java standards. - You can use subList without constructing a new ArrayList from it.

This code shows that.

import java.util.*;
import java.util.stream.*;

public class A {
    public static void main(String[] args) {
        int upperLimit = 30000;
        // Create ids in next line just to illustrate  a list of 30000 items
        List<String> ids = IntStream.range(0, upperLimit).mapToObj(x -> "" + x).collect(Collectors.toList());

        int step = 1000;
        for (int i = 0; i < upperLimit; i += step) {
            List<String> ga = ids.subList(i, i + step);
            checkSubList(ga);
        }
    }

    private static void checkSubList(List<String> ga) {
        //  implement this as you'd like
        System.out.println(ga);
    }
}
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147