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;
    }
    
}

Java技术学习 https://www.itkc8.com

 

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();
        }
    }
    
}

Java技术学习 https://www.itkc8.com

 

 

 

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java 8的Stream API和Collectors工具类来实现集合分组并按照数量排序的操作。具体步骤如下: 1. 使用`Collectors.groupingBy`方法对集合进行分组分组的依据可以是元素本身或者根据某个属性进行分组。例如,以下代码将一个字符串集合按照首字母进行分组: ``` Map<Character, List<String>> groupMap = list.stream() .collect(Collectors.groupingBy(s -> s.charAt(0))); ``` 2. 使用`Collectors.counting`方法对分组后的每个组进行计数,得到每个组的元素数量。例如,以下代码将上一步得到的分组Map按照组内元素数量进行计数: ``` Map<Character, Long> countMap = groupMap.entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> (long) e.getValue().size() )); ``` 3. 使用Java 8新的`Comparator`接口,定义一个按照Map中值的大小进行排序的比较器。例如,以下代码定义一个按照countMap中值的大小进行排序的比较器: ``` Comparator<Character> comparator = Comparator.comparing(countMap::get); ``` 4. 使用`Collectors.toMap`方法将排序后的结果转换为一个新的Map。例如,以下代码将countMap按照值的大小进行排序,并将结果保存在一个新的Map中: ``` Map<Character, Long> sortedMap = countMap.entrySet().stream() .sorted(Map.Entry.comparingByValue(comparator.reversed())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new )); ``` 最后,得到的`sortedMap`就是按照分组后每个组的元素数量进行排序的结果。 完整代码示例: ``` List<String> list = Arrays.asList("apple", "banana", "cat", "dog", "elephant", "fig", "goat", "hippo"); Map<Character, List<String>> groupMap = list.stream() .collect(Collectors.groupingBy(s -> s.charAt(0))); Map<Character, Long> countMap = groupMap.entrySet().stream() .collect(Collectors.toMap( Map.Entry::getKey, e -> (long) e.getValue().size() )); Comparator<Character> comparator = Comparator.comparing(countMap::get); Map<Character, Long> sortedMap = countMap.entrySet().stream() .sorted(Map.Entry.comparingByValue(comparator.reversed())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new )); System.out.println(sortedMap); // 输出: {e=2, a=1, b=1, c=1, d=1, f=1, g=1, h=1} ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值