HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Cutting it to Pages

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pagescuttingstackoverflow

Problem

I was working on representing a large List of information on a number of different pages, and a colleague of mine suggested that I try to make a generic method for the task I was trying to perform:

private  List> listSplitter(List originalList, int splitCount) {
    List> listOfLists= new ArrayList<>();
    listOfLists.add(new ArrayList<>());

    int originalListSize = originalList.size();     
    int index = 0;
    int pageNumber = 0;
    int numItemsAdded = 0;

    while (index  splitCount - 1) {
            numItemsAdded = 0;
            pageNumber ++;
            listOfLists.add(new ArrayList<>());
        }
        List activeList = listOfLists.get(pageNumber);
        activeList.add(originalList.get(index));
        numItemsAdded++;
        index++;
    }

    return listOfLists;
}


This correctly splits a list of objects into any number of lists based on the desired number of objects per list. I would love to get feedback on this method, as I am new to Java generics.

Solution

Compiler Warning

This line gives you a compiler warning because it's not using generics:

List activeList = listOfLists.get(pageNumber);


it should be

List activeList = listOfLists.get(pageNumber);


Empty list input

If the input is an empty list, then the output will be a list containing one empty list. I think it would be more reasonable if the output itself was an empty list.

This can be solved by only adding a list if the number of items added is more than zero.

splitCount splitCount - 1) {

This if-statement would be a lot easier to grasp if it was

if (numItemsAdded >= splitCount) {


There is no need to introduce the - 1 when you can just change the operator.

My final code:

With the above in mind, here's what I ended up with:

private static  List> listSplitter(List originalList, int resultsPerList) {
    if (resultsPerList > listOfLists = new ArrayList<>();
    List latestList = new ArrayList<>();
    Iterator iterator = originalList.iterator();
    
    while (iterator.hasNext()) {
        T next = iterator.next();
        if (latestList.size() >= resultsPerList) {
            listOfLists.add(latestList);
            latestList = new ArrayList();
        } 
        latestList.add(next);
    }
    
    if (!latestList.isEmpty()) {
        listOfLists.add(latestList);
    }

    return listOfLists;
}

Code Snippets

List activeList = listOfLists.get(pageNumber);
List<T> activeList = listOfLists.get(pageNumber);
if (numItemsAdded >= splitCount) {
private static <T> List<List<T>> listSplitter(List<T> originalList, int resultsPerList) {
    if (resultsPerList <= 0) {
        throw new IllegalArgumentException("resultsPerList must be positive");
    }
    List<List<T>> listOfLists = new ArrayList<>();
    List<T> latestList = new ArrayList<>();
    Iterator<T> iterator = originalList.iterator();
    
    while (iterator.hasNext()) {
        T next = iterator.next();
        if (latestList.size() >= resultsPerList) {
            listOfLists.add(latestList);
            latestList = new ArrayList<T>();
        } 
        latestList.add(next);
    }
    
    if (!latestList.isEmpty()) {
        listOfLists.add(latestList);
    }

    return listOfLists;
}

Context

StackExchange Code Review Q#65232, answer score: 11

Revisions (0)

No revisions yet.