Sunday, February 26, 2012

Collection Splitting / Partition algorithm

Collection Splitting / Partition algorithm

//Line used to call split method
List<List<?>> txnBatchPendingLists = split(originalListToSplit, targetSize); //targetSize=4 or anything

//Method of split
public static List<List<?>> split(List<?> originalListToSplit, int targetSize) {
    List<List<?>> splittedLists = new ArrayList<List<?>>();
        
    for (int i = 0; i < originalListToSplit.size(); i += targetSize) {
       List tokenList=new ArrayList( 
                originalListToSplit.subList(i, Math.min(i + targetSize, originalListToSplit.size()) ));
       
       splittedLists.add(tokenList);
    }
    return splittedLists;
}
-----------End---------

No comments:

Post a Comment