2

I have the following problem:

I have a method as follows that can take a collection of only upto 500 Names at a time:

public Optional<ResultDTO> executeRequest(final int time, List<Name> names)

I have a list 1000+ names and hence I want to partition this list out into sublists of max size 500 using guava

List<List<Name>> nameBatches = Lists.partition(namesList, 500);

and pass it to the request to get a list of ResultDTOs. How can I do this in Java8?

Jan Chimiak
  • 736
  • 7
  • 20

2 Answers2

0

I'm not sure if in the java 8 similar functionality has been provided. Of course you can use foreach with anonymous function but it's more unreadable than in the java 8 with using List.subList().

Solution in the Java 7:

        final Integer sizeOfChunk = 3;
        List<Integer> inputList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

        //Guava
        List<List<Integer>> firstListOfLists = Lists.partition(inputList, sizeOfChunk);

        //Java 7
        List<List<Integer>> secondListOfLists = new ArrayList<List<Integer>>();
        for (int i=0; i<inputList.size()-sizeOfChunk; i+=sizeOfChunk) {
            secondListOfLists.add(inputList.subList(i, i+sizeOfChunk));
        }
        if (inputList.size() % sizeOfChunk != 0) {
            secondListOfLists.add(inputList.subList(secondListOfLists.size()*sizeOfChunk, inputList.size()));
        }

        System.out.println("Guava: " +  firstListOfLists);
        System.out.println("Java 7: " +  secondListOfLists);

EDIT

After your comment, it will be:

List<Optional<ResultDTO>> results = nameBatches .stream().map(l -> executeRequest(something, l)).collect(Collectors.toList()); 
Giver
  • 91
  • 2
  • 10
0

You will have to create an IntStream to iterate over the chunks in your list and create sub-lists in a separate mapping step.

int chunkSize = 500;
int size = namesList.size();
// calc the number of chunks
int chunks = (size - 1) / chunkSize;

List<List<Name>> res = IntStream
    // range over all chunks
    .range(0, chunks + 1)
    // create sub-lists for each chunk
    .mapToObj(
        n -> namesList.subList(n * chunkSize, Math.min(size, (n + 1) * chunkSize)))
    // collect from Stream<List<String>> to List<List<String>>
    .collect(Collectors.toList());

Edit: based on your comment

My main issue is how do I call the executeRequest method for each of the name batches and store the results.

Just add an .map(l -> executeRequest(time, l)) before the collect call, and change the type of res to List<Optional<Object>>

nyname00
  • 2,496
  • 2
  • 22
  • 25