zoukankan      html  css  js  c++  java
  • 将list分成等数量

    import java.util.ArrayList;
    import java.util.List;
    
    public class CollectionGroupUtil {
        
        public static List groupListByQuantity(List list, int quantity) {
            if (list == null || list.size() == 0) {
                return list;
            }
            
            if (quantity <= 0) {
                new IllegalArgumentException("Wrong quantity.");
            }
            
            List wrapList = new ArrayList();
            int count = 0;
            while (count < list.size()) {
                wrapList.add(list.subList(count, (count + quantity) > list.size() ? list.size() : count + quantity));
                count += quantity;
            }
            
            return wrapList;
        }
        
    }

    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    
    public class CollectionGroupUtilTest {
        
        /**
         * 大于分组数量的情况
         */
        @Test
        public void test() {
            List<String> allList = new ArrayList<String>();
            for (int i = 1; i <= 504; i++) {
                allList.add(i  + "");
            }
            
            List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
            int i = 0;
            List<String> list = null;
            for (int c = 0; c < groupList.size(); c++) {
                list = groupList.get(c);
                
                System.out.println("第" + (c + 1) + "组: ");
                for (String temp : list) {
                    System.out.print(temp + ", ");
                }
                System.out.println();
            }
        }
    
        /**
         * 小于分组数量的情况
         */
        @Test
        public void test2() {
            List<String> allList = new ArrayList<String>();
            for (int i = 1; i <= 45; i++) {
                allList.add(i  + "");
            }
            
            List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
            int i = 0;
            List<String> list = null;
            for (int c = 0; c < groupList.size(); c++) {
                list = groupList.get(c);
                
                System.out.println("第" + (c + 1) + "组: ");
                for (String temp : list) {
                    System.out.print(temp + ", ");
                }
                System.out.println();
            }
        }
        
        /**
         * 集合只有一个记录的情况
         */
        @Test
        public void test3() {
            List<String> allList = new ArrayList<String>();
            for (int i = 1; i <= 1; i++) {
                allList.add(i  + "");
            }
            
            List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
            int i = 0;
            List<String> list = null;
            for (int c = 0; c < groupList.size(); c++) {
                list = groupList.get(c);
                
                System.out.println("第" + (c + 1) + "组: ");
                for (String temp : list) {
                    System.out.print(temp + ", ");
                }
                System.out.println();
            }
        }
        
        /**
         * 空集合的情况
         */
        @Test
        public void test4() {
            List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(null, 50);
            System.out.println(groupList);
            
            groupList = CollectionGroupUtil.groupListByQuantity(new ArrayList(), 50);
            System.out.println(groupList);
        }
        
        /**
         * 集合刚满一个分组的情况
         */
        @Test
        public void test5() {
            List<String> allList = new ArrayList<String>();
            for (int i = 1; i <= 50; i++) {
                allList.add(i  + "");
            }
            
            List<List<String>> groupList = CollectionGroupUtil.groupListByQuantity(allList, 50);
            int i = 0;
            List<String> list = null;
            for (int c = 0; c < groupList.size(); c++) {
                list = groupList.get(c);
                
                System.out.println("第" + (c + 1) + "组: ");
                for (String temp : list) {
                    System.out.print(temp + ", ");
                }
                System.out.println();
            }
        }
        
    }
  • 相关阅读:
    前端综合练习与bootstrap
    05-前端之jQuery
    03-初识JavaScript
    02-初识CSS
    01-前端初识和body标签中的相关标签
    程序员笔记
    windows和Ubantu双系统安装图解
    获取http和ftp地址的图片
    HttpHelper类及调用
    简单的SqlHelper
  • 原文地址:https://www.cnblogs.com/hujinshui/p/8053942.html
Copyright © 2011-2022 走看看