簡體   English   中英

有一種簡單的方法可以將一個列表拆分為X個子列表嗎?

[英]Is there a simple way to split ONE list into X sublists?

我有一個大於20k的隨機大小列表。 如何將它們拆分為子列表,其中每個子列表的長度都相等或長度等於1(對於奇數列表?)?

由於它是隨機大小,因此實現中應該沒有任何定義的大小,對嗎?

我目前正在查看此模板:

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的記錄和sublistSize 5的列表。最后我得到了5個子列表。 前四個子列表將有5269條記錄,最后(第5個)子列表將有5270條記錄。

這個怎么樣? 這將按照您說的做(如果項目的順序不重要),則創建“大小”子列表,並將所有項目分發到新列表中。

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

這是使用sublist方法進行優化的Hamsar路徑的改進。

   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;

}

如果要維護每個子列表中大列表的順序,請嘗試以下操作:

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

這將根據子列表的所需大小將主列表拆分為子列表。

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

}

嘗試使用此方法來維護子列表中主列表的順序。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM