2013-05-17 3 views
0

20k보다 큰 임의의 크기 목록이 있습니다. 각 하위 목록의 길이가 동일하거나 길이가 같을 경우 하위 목록으로 나누는 방법은 무엇입니까 (홀수 목록의 경우?)?하나의 목록을 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와 target sublistSize를 전달하는 것입니다. 그래서 크기 26346 레코드와 sublistSize 5 목록을 전달합니다. 나는 5 sublists 결국 것입니다. 처음 네 개의 하위 목록에는 5269 개의 ​​레코드가 있고 마지막 다섯 번째 하위 목록에는 5270 개의 레코드가 있습니다.

+0

특정 길이의 특정 하위 목록 또는 각 하위 목록을 원하십니까? – kuporific

+0

순간에 나는 최대 5 개의 목록을 원할 것이고, 각 목록에는 대략 동일한 번호 레코드가 있습니다. – iCodeLikeImDrunk

+0

하위 목록의 길이는 '길이'이고 일부는 길이가 길이 + 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; 
}