Loading
Java split ArrayList to multiple List

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Java split ArrayList to multiple


Ans)

Split List into Smaller Lists.

This example Splits the given big list to smaller
lists based in Element Size.

   /**
     * The method returns List of ArrayLists having 500 objects in each list.
     * @param dataList the dataList
     * @param numElements the num elements
     * @return the list[]
     */
    public static List[] splitArrayListIntoSmall(ArrayList dataList, int numElements)
    {
        if(dataList == null || dataList.size() == 0 || numElements == 0)
            return new List[0];
        if(numElements < 0)
            return (new List[] {
                    dataList
            });
        int numLists = (dataList.size() % numElements) == 0?(dataList.size() /
  numElements):((dataList.size() / numElements)+ 1);
        List result[] = new List[numLists];
        for(int i = 0; i < numLists; i++)
        {
            int count = i + 1;
            int fromIndex = Math.max((count - 1) * dataList.size()/numLists, 0);
            int toIndex = Math.min(count * dataList.size()/numLists, dataList.size());
            result[i] = dataList.subList(fromIndex, toIndex);
        }
        return result;
    }



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Java split ArrayList to multiple List


Java split ArrayList to multiple List

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free