Java List Split Test

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;

/**
 * Project Name:FireWeb
 * File Name:TestListSplit.java
 * Package Name:
 *Date: October 16, 2017 3:13:01 PM
 *
*/

/**
 * ClassName:TestListSplit <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date: October 16, 2017 3:13:01 PM <br/>
 * @author   sql
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
public class TestListSplit {
	
	private static List<Integer> createList(int len){
		List<Integer> l = new ArrayList<Integer>();
		for (int i = 0; i < len; i++) {
			l.add(i);
		}
		return l;
	}
	
	 /**
     * Split List
     * @param list The list to be split
     * @param pageSize The size of each list
     * @return List<<List<T>>
     */
     public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
        
        int listSize = list.size(); //size of list
        int page = (listSize + (pageSize-1))/ pageSize;                      //页数
        
        List<List<T>> listArray = new ArrayList<List<T>>(); //Create a list array to save the divided list
        for(int i=0;i<page;i++) { //traverse according to the size of the array
            List<T> subList = new ArrayList<T>(); //Each bit of the array is put into a divided list
            for(int j=0;j<listSize;j++) { //traverse the list to be divided
                int pageIndex = ( (j + 1) + (pageSize-1) ) / pageSize; //The page number of the current record (page number)
                if(pageIndex == (i + 1)) { //When the page number of the current record is equal to the page number to be placed
                    subList.add(list.get(j)); //Put the elements in the list to the divided list(subList)
                }
                
                if( (j + 1) == ((j + 1) * pageSize) ) { //Exit the current loop when one page is full
                    break;
                }
            }
            listArray.add(subList); //Put the split list into the bits of the corresponding array
        }
        return listArray;
    }
	
	public static void main(String[] args) {
		List<Integer> l  = createList(100000);
		System.out.println(l.size());
		List<List<Integer>> sl = splitList(l, 500);
		System.out.println(sl.size());
		for (int i = 0; i < sl.size(); i++) {
			List<Integer> subl = sl.get(i);
			System.out.print(i + " [" + subl.size()+"]");
			for (Integer si : subl) {
				System.out.print(" ," + si);
			}
			System.out.println("\r");
		}
		
//		System.out.println(33%10);
	}

}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326680308&siteId=291194637