Java: split a List into two sub-Lists?

后端 未结 14 1532
执笔经年
执笔经年 2020-12-08 02:29

What\'s the simplest, most standard, and/or most efficient way to split a List into two sub-Lists in Java? It\'s OK to mutate the original List, so no copying should be nece

14条回答
  •  粉色の甜心
    2020-12-08 02:58

    import java.util.Collection;
    
    public class CollectionUtils {
    
      /**
       * Will split the passed collection so that the size of the new collections
       * is not greater than maxSize
       * @param t
       * @param maxSize
       * @return a List containing splitted collections
       */
    
      @SuppressWarnings("unchecked")
      public static  List>split(Collection t, int maxSize) {
        int counter = 0;
        List> ret = new LinkedList>();
        Iteratoritr = t.iterator();
        try {
          Collection tmp = t.getClass().newInstance();
          ret.add(tmp);
          while(itr.hasNext()) {
            tmp.add(itr.next());
            counter++;
            if(counter>=maxSize && itr.hasNext()) {
              tmp = t.getClass().newInstance();
              ret.add(tmp);
              counter=0;
            }
          }
        } catch(Throwable e) {
          Logger.getLogger(CollectionUtils.class).error("There was an error spliting "+t.getClass(),e);
        }
        return ret;
      }
    
    }
    
    // JUnit test cases
    
    import java.util.ArrayList;
    
    /**
     *
     * $Change$
     * @version $Revision$
     * Last modified date & time $DateTime$
     */
    public class CollectionUtilsTest {
    
      @Test
      public void testSplitList() {
        Listtest = new ArrayList(100);
        for (int i=1;i<101;i++) {
          test.add(i);
        }
        List> tests = CollectionUtils.split(test, 10);
        Assert.assertEquals("Size mismatch", 10,tests.size());
    
        TreeSet tmp = new TreeSet();
        for(Collection cs:tests) {
          for(Integer i:cs) {
            Assert.assertFalse("Duplicated item found "+i,tmp.contains(i));
            tmp.add(i);
          }
          System.out.println(cs);
        }
        int why = 1;
        for(Integer i:tmp) {
          Assert.assertEquals("Not all items are in the collection ",why,i.intValue());
          why++;
        }
      }
      @Test
      public void testSplitSet() {
        TreeSettest = new TreeSet();
        for (int i=1;i<112;i++) {
          test.add(i);
        }
        List> tests = CollectionUtils.split(test, 10);
        Assert.assertEquals("Size mismatch", 12,tests.size());
    
        TreeSet tmp = new TreeSet();
        int cI = 0;
        for(Collection cs:tests) {
          for(Integer i:cs) {
            Assert.assertFalse("Duplicated item found "+i,tmp.contains(i));
            tmp.add(i);
          }
    //      if(cI>10) {
            System.out.println(cs);
    //      }
          cI++;
        }
        int why = 1;
        for(Integer i:tmp) {
    
          Assert.assertEquals("Not all items are in the collection ",why,i.intValue());
          why++;
        }
      }
    }
    

提交回复
热议问题