zoukankan      html  css  js  c++  java
  • Split / Partition a collection into smaller collections

     

    Partition a collection into smaller collections

    This article describes how to partition a collections into a given number of smaller collections.

     

    1. Partition a collection

    Sometime you want split a collection into smaller collections. The following gives an example how to do this.

    Tip

     Please note that this example is an adjusted example of the Google collection and more or less the same as Code Ranch discussion The Google source code can be found here Original source code

    The test code also demonstrate how to calculate the number of elements which should go into one bucket in case you want to have a fixed number of buckets.

     

    2. Partition collection in Java

    2.1. Implementation

    Create a Java project "de.vogella.algorithms.partitioncollection".

    Create the following program.

    package de.vogella.algorithms.partitioncollection;
    
    import java.util.AbstractList;
    import java.util.List;
    
    
    public class MyPartition {
    /** * Returns consecutive {@linkplain List#subList(int, int) sublists} of a list, * each of the same size (the final list may be smaller). For example, * partitioning a list containing {@code [a, b, c, d, e]} with a partition * size of 3 yields {@code [[a, b, c], [d, e]]} -- an outer list containing * two inner lists of three and two elements, all in the original order. * * <p>The outer list is unmodifiable, but reflects the latest state of the * source list. The inner lists are sublist views of the original list, * produced on demand using {@link List#subList(int, int)}, and are subject * to all the usual caveats about modification as explained in that API. * * * Adapted from http://code.google.com/p/google-collections/ * * @param list the list to return consecutive sublists of * @param size the desired size of each sublist (the last may be * smaller) * @return a list of consecutive sublists * @throws IllegalArgumentException if {@code partitionSize} is nonpositive * */
    
    
        
        public static <T> List<List<T>> partition(List<T> list, int size) {
       
         if (list == null)
            throw new NullPointerException("'list' must not be null");
          if (!(size > 0))
            throw new IllegalArgumentException("'size' must be greater than 0");
    
          return new Partition<T>(list, size);
        }
    
        private static class Partition<T> extends AbstractList<List<T>> {
    
          final List<T> list;
          final int size;
    
          Partition(List<T> list, int size) {
            this.list = list;
            this.size = size;
          }
    
          @Override
          public List<T> get(int index) {
            int listSize = size();
            if (listSize < 0)
              throw new IllegalArgumentException("negative size: " + listSize);
            if (index < 0)
              throw new IndexOutOfBoundsException("index " + index + " must not be negative");
            if (index >= listSize)
              throw new IndexOutOfBoundsException("index " + index + " must be less than size " + listSize);
            int start = index * size;
            int end = Math.min(start + size, list.size());
            return list.subList(start, end);
          }
    
          @Override
          public int size() {
            return (list.size() + size - 1) / size;
          }
    
          @Override
          public boolean isEmpty() {
            return list.isEmpty();
          }
        }
    
    
    } 
     
    

      

    2.2. Test

    You can use the following JUnit test to validate the result.

    package de.vogella.algorithms.partitioncollection;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    import static org.junit.Assert.assertTrue;
    
    
    public class MyPartitionTest {
      @Test
      public void partitiontest1() {
        List<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("six");
        list.add("seven");
        list.add("eight");
        list.add("nine");
        list.add("ten");
        list.add("eleven");
        List<List<String>> partition = MyPartition.partition(list, 1);
        System.out.println(partition.get(2).size()); 
        assertTrue(partition.size()==11);
        assertTrue(partition.get(0).size()==1);
        partition = MyPartition.partition(list, 7);
        assertTrue(partition.size()==2);
        assertTrue(partition.get(0).size()==7);
        assertTrue(partition.get(1).size()==4);
        
        // now let assume you want to have x number of buckets
        // How many elements must placed in a list?
        // Take x as 3
        
        int buckets = 3;
        int divide = list.size() / buckets;  
        if (list.size() % buckets !=0){
          divide ++;
        }
        System.out.println("Max. number of element in each bucket " + divide);
        partition = MyPartition.partition(list, divide);
        assertTrue(partition.size()==buckets);
      }
    } 
     
    

      

  • 相关阅读:
    Python3基础 try-except 几个异常采取同样的处理方法
    Python3基础 try-except else进行配合
    客户端(Winform窗体)上传文件到服务器(web窗体)简单例子
    运用Microsoft.DirectX.DirectSound和Microsoft.DirectX实现简单的录音功能
    Microsoft.DirectX.DirectSound.dll和Microsoft.DirectX.dll引用,导致项目无法调试问题
    asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法
    系统中怎么删除右键新建菜单中多余的选项
    win10家庭版无法打开系统内置应用(录音机、日历等),如何解决“内置管理员无法打开此应用”的问题
    div中背景图片自动适应屏幕高度无效原因和例子
    asp.net.web如何简单生成和保存二维码图片的例子
  • 原文地址:https://www.cnblogs.com/hephec/p/4579648.html
Copyright © 2011-2022 走看看