2013-05-17 54 views
0

我有一个随机大小大于20k的列表。我怎样才能将它们分成子列表,其中每个子列表将具有相同的长度或等于+1(奇数列表?)?有没有简单的方法将ONE列表拆分为X个子列表?

由于它是随机大小,实现应该没有任何定义的大小正确吗?

我目前正在研究这个模板:

public static <T> List<List<T>> split(List<T> list, int size) throws NullPointerException, IllegalArgumentException { 
     if (list == null) { 
      throw new NullPointerException("The list parameter is null."); 
     } 
     if (size <= 0) { 
      throw new IllegalArgumentException("The size parameter must be more than 0."); 
     } 

     int num = list.size()/size; 
     int mod = list.size() % size; 
     List<List<T>> ret = new ArrayList<List<T>>(mod > 0 ? num + 1 : num); 
     for (int i = 0; i < num; i++) { 
      ret.add(list.subList(i * size, (i + 1) * size)); 
     } 
     if (mod > 0) { 
      ret.add(list.subList(num * size, list.size())); 
     } 
     return ret; 
    } 

这一个是创建子列表基于已知子表的大小,然后创建X子列表。

我需要的结果是传递一个LIST和一个目标sublistSize。所以我通过一个大小为26346的记录和子列表大小为5的列表。最终我会得到5个子列表。前四个子列表将包含5269条记录,最后一个(第五个)子列表将包含5270条记录。

+0

你想要一定数量的子列表或每个子列表有一定的长度吗? – kuporific

+0

此刻我最多需要5个列表,然后在每个列表中都有大约相同的数字记录。 – iCodeLikeImDrunk

+0

你提到子列表将有一个长度“length”,有些将有一个长度“length + 1”。这些超长列表是否应该是第一个子列表,最后一个子列表,其他子列表以及其他一些安排? – kuporific

回答

7

这个怎么样?这将完成你所说的(如果项目顺序不重要),创建'尺寸'子列表,并将所有项目分配到新列表中。

public static <T> List<List<T>> split(List<T> list, int size) 
     throws NullPointerException, IllegalArgumentException { 
    if (list == null) { 
     throw new NullPointerException("The list parameter is null."); 
    } 

    if (size <= 0) { 
     throw new IllegalArgumentException(
       "The size parameter must be more than 0."); 
    } 

    List<List<T>> result = new ArrayList<List<T>>(size); 

    for (int i = 0; i < size; i++) { 
     result.add(new ArrayList<T>()); 
    } 

    int index = 0; 

    for (T t : list) { 
     result.get(index).add(t); 
     index = (index + 1) % size; 
    } 

    return result; 
} 
0

如果你想保持在每个子列表的大型列表的顺序,请尝试以下操作:

public static <T> List<List<T>> split(List<T> list, int numberOfLists) { 
    if (list == null) { 
     throw new NullPointerException("The list parameter is null."); 
    } 
    if (numberOfLists <= 0) { 
     throw new IllegalArgumentException(
       "The number of lists parameter must be more than 0."); 
    } 

    int sizeOfSubList = list.size()/numberOfLists + 1; 
    int remainder = list.size() % numberOfLists; 

    List<List<T>> subLists = new ArrayList<List<T>>(numberOfLists); 

    // if there is a remainder, let the first sub-lists have one length... 
    for (int i = 0; i < numberOfLists - remainder; i++) { 
     subLists.add(list.subList(i*sizeOfSubList, (i+1)*sizeOfSubList)); 
    } 

    // ... the remaining sub-lists will have -1 size than the first. 
    sizeOfSubList--; 
    for (int i = numberOfLists - remainder; i < numberOfLists; i++) { 
     subLists.add(list.subList(i*sizeOfSubList, (i+1)*sizeOfSubList)); 
    } 

    return subLists; 
} 
+0

如果我正确读了这个,这实际上会创建6个子列表。 – iCodeLikeImDrunk

0

这将根据所需大小的主列表分成子列表的子列表。

public List splitListToSubList(List<Object> parentList, int childListSize) { 
    List<List<Object>> childList = new ArrayList<List<Object>>(); 
    List<Object> tempList = new ArrayList<Object>(); 
    int count = 0; 
    if (parentList != null) { 
     for (Object obj : parentList) { 
      if (count < childListSize) { 
       count = count + 1; 
       tempList.add(obj); 
      } else { 
       childList.add(tempList); 
       tempList = new ArrayList<Object>(); 
       tempList.add(obj); 
       count = 1; 
      } 

     } 
     if (tempList.size() < childListSize) { 
      childList.add(tempList); 
     } 
    } 
    return childList; 
} 

}

1

这是使用优化的sublist方法Hamsar的aproch的改善。

public static <T> List<List<T>> splitListToSubLists(List<T> parentList, int subListSize) { 
    List<List<T>> subLists = new ArrayList<List<T>>(); 
    if (subListSize > parentList.size()) { 
    subLists.add(parentList); 
    } else { 
    int remainingElements = parentList.size(); 
    int startIndex = 0; 
    int endIndex = subListSize; 
    do { 
     List<T> subList = parentList.subList(startIndex, endIndex); 
     subLists.add(subList); 
     startIndex = endIndex; 
     if (remainingElements - subListSize >= subListSize) { 
      endIndex = startIndex + subListSize; 
     } else { 
      endIndex = startIndex + remainingElements - subList.size(); 
     } 
     remainingElements -= subList.size(); 
    } while (remainingElements > 0); 

    } 
    return subLists; 

}

0

尝试一下本作mantaining在子列表主列表的顺序。

public <T> List<List<T>> orderedSplit(List<T> list, int lists) throws NullPointerException, IllegalArgumentException { 
    if (list == null) { 
     throw new NullPointerException("La lista es nula."); 
    } 

    if (lists <= 0) { 
     throw new IllegalArgumentException("La lista debe divirse en una cantidad mayor a 0."); 
    } 

    if(list.size() < lists){ 
     throw new IllegalArgumentException("El tamaño de la lista no es suficiente para esa distribución."); 
    } 

    List<List<T>> result = new ArrayList<List<T>>(lists); 

    int listsSize = list.size()/lists; 
    int remainder = list.size() % lists; 

    int index = 0; 
    int remainderAccess = 0; 
    int from = index*listsSize + remainderAccess; 
    int to = (index+1)*listsSize + remainderAccess; 

    while(lists > index){ 

     if(remainder != 0){ 
      result.add(list.subList(from, to+1)); 
      remainder--; 
      remainderAccess++; 
     }else { 
      result.add(list.subList(from, to)); 
     } 

     index++; 
     from = index*listsSize + remainderAccess; 
     to = (index+1)*listsSize + remainderAccess; 
    } 

    return result; 
} 
相关问题