Java-调整嵌套列表的数量
我需要一些代码样本或算法来调整Java-调整嵌套列表的数量,java,nested-lists,Java,Nested Lists,我需要一些代码样本或算法来调整列表的大小,这应该可以用下一种方式工作: 让我们设想下一个newSize和incomingList(伪代码): 如果newSize是奇数(例如3),则下一步是: newSize始终大于incomingList.size() 如果您有任何建议,我将不胜感激 更新 在google.common.list的帮助下,我已经完成了代码草案(是的,它闻起来很香),我希望它能帮助别人 在我的例子中,方法接收不同的incomingList.size()s和newSizeparams
列表的大小
,这应该可以用下一种方式工作:
让我们设想下一个newSize
和incomingList
(伪代码):
如果newSize是奇数(例如3),则下一步是:
newSize
始终大于incomingList.size()
如果您有任何建议,我将不胜感激
更新
在google.common.list
的帮助下,我已经完成了代码草案(是的,它闻起来很香),我希望它能帮助别人
在我的例子中,方法接收不同的incomingList.size()
s和newSize
params,很明显,incomingList.size()/newSize
将返回双倍值(例如传入list.size()=1000,但我们需要将其“压缩”到600个元素),因此我不能一直使用Lists.partition
<代码>扩展列表最好在下一个代码后调用:
int maxSplitValue = (int) Math.ceil((double) incomingList.size() / newSize);
List<List<Integer>> firstPortionValues = Lists.partition(
incomingList, maxSplitValue
);//size can be less than required after double to int upper round
if (firstPortionValues.size() < maxSplitValue) {
List<List<Integer>> expandedList = expandList(firstPortionValues, maxSplitValue)
}
代码:
public List expandList(List incomingList,int newSize){
List resultList=new ArrayList();
对于(int index=0;index1){
int-maxPortionValue=listElementSize%2==0?listElementSize/2:--listElementSize;
布尔ISOKUSEMExportionValue=maxPortionValue+listSize-index+resultListSize 0){
addAll(list.partition(nodeList,minSplitValue));
返回结果;
}否则{
结果。添加(节点列表);
返回结果;
}
}
为什么不使用ListUtils.union(list1,list2);
from和
为什么不使用
ListUtils.union(list1,list2);
from和
阅读您的问题,我可以想出执行两个步骤的算法思想:
@Test
public void test(){
// Init lists
List<Integer> a = Lists.newArrayList(1,2,3);
List<Integer> b = Lists.newArrayList(4,5,6);
List<Integer> c = Lists.newArrayList(7,8,9);
List<List<Integer>> incomingList = Lists.newArrayList(a,b,c);
System.out.println(incomingList);
// Create combined list
Iterable<Integer> tempList = Iterables.concat(incomingList);
// Re-Partition list
Iterable<List<Integer>> result = Iterables.partition(tempList, 2); // New size: 2
// Convert from Iterables to List
List<List<Integer>> finalList = Lists.newArrayList(result);
System.out.println(finalList);
}
上述代码易于调试,您可以减少代码的行数,还可以利用import static使其更具可读性,并实现以下功能:
import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Lists.*;
public void test(){
List<Integer> a = newArrayList(1,2,3);
List<Integer> b = newArrayList(4,5,6);
List<Integer> c = newArrayList(7,8,9);
List<List<Integer>> incomingList = newArrayList(a,b,c);
System.out.println(incomingList);
// Repartition
List<List<Integer>> finalList = newArrayList(partition(concat(incomingList), 2));
System.out.println(finalList);
}
import static com.google.common.collect.Iterables.*;
导入静态com.google.common.collect.Lists.*;
公开无效测试(){
列表a=newArrayList(1,2,3);
列表b=newArrayList(4,5,6);
列表c=newArrayList(7,8,9);
列表incomingList=newArrayList(a、b、c);
系统输出打印项次(收入表);
//再分配
List finalList=newArrayList(分区(concat(incomingList),2));
System.out.println(finalList);
}
作为结果列表上的一个注释,分区方法创建了多个N值列表,但最后一个列表的值可能会更少。因为您在开始时所说的似乎是您想要的值更少。我将这留给您来搜索分区方法和番石榴用法。阅读您的问题,我可以想出执行两个步骤的算法思想:
@Test
public void test(){
// Init lists
List<Integer> a = Lists.newArrayList(1,2,3);
List<Integer> b = Lists.newArrayList(4,5,6);
List<Integer> c = Lists.newArrayList(7,8,9);
List<List<Integer>> incomingList = Lists.newArrayList(a,b,c);
System.out.println(incomingList);
// Create combined list
Iterable<Integer> tempList = Iterables.concat(incomingList);
// Re-Partition list
Iterable<List<Integer>> result = Iterables.partition(tempList, 2); // New size: 2
// Convert from Iterables to List
List<List<Integer>> finalList = Lists.newArrayList(result);
System.out.println(finalList);
}
上述代码易于调试,您可以减少代码的行数,还可以利用import static使其更具可读性,并实现以下功能:
import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Lists.*;
public void test(){
List<Integer> a = newArrayList(1,2,3);
List<Integer> b = newArrayList(4,5,6);
List<Integer> c = newArrayList(7,8,9);
List<List<Integer>> incomingList = newArrayList(a,b,c);
System.out.println(incomingList);
// Repartition
List<List<Integer>> finalList = newArrayList(partition(concat(incomingList), 2));
System.out.println(finalList);
}
import static com.google.common.collect.Iterables.*;
导入静态com.google.common.collect.Lists.*;
公开无效测试(){
列表a=newArrayList(1,2,3);
列表b=newArrayList(4,5,6);
列表c=newArrayList(7,8,9);
列表incomingList=newArrayList(a、b、c);
系统输出打印项次(收入表);
//再分配
List finalList=newArrayList(分区(concat(incomingList),2));
System.out.println(finalList);
}
作为结果列表上的一个注释,分区方法创建了多个N值列表,但最后一个列表的值可能会更少。因为您在开始时所说的似乎是您想要的值更少。我将这留给您来搜索分区方法和番石榴用法。您可以对其进行编码,纯Java7。此代码保持新列表的平衡,添加最后是额外的元素
public List<List<Integer>> resizeListOfNestedList(int newSize, List<List<Integer>> data) {
ArrayList<Integer> allElements = new ArrayList<>();
for (List<Integer> integers : data) {
allElements.addAll(integers);
}
int elementsPerItem = allElements.size() / newSize;
int extraElements = allElements.size() % newSize;
int indexToStartAddExtraElement = newSize - extraElements;
ArrayList<List<Integer>> result = new ArrayList<>(newSize);
Iterator<Integer> iterator = allElements.iterator();
for (int i = 0; i < newSize; i++){
int currentItemElementsCount = elementsPerItem;
if (i >= indexToStartAddExtraElement)
currentItemElementsCount++;
ArrayList<Integer> current = new ArrayList<>(currentItemElementsCount);
for (int j = 0; j < currentItemElementsCount; j++){
current.add(iterator.next());
}
result.add(current);
}
return result;
}
public List resizeListOfNestedList(int newSize,列表数据){
ArrayList等位基因=新的ArrayList();
for(列出整数:数据){
等位基因。addAll(整数);
}
int-elementsPerItem=allegements.size()/newSize;
int extracelements=allegements.size()%newSize;
int indexToStartAddExtraElement=newSize-extraElements;
ArrayList结果=新建ArrayList(newSize);
迭代器迭代器=等位基因。迭代器();
对于(int i=0;i=IndexStatardExtraElement)
CurrentItemElementScont++;
ArrayList current=新的ArrayList(currentItemElementsCount);
对于(int j=0;j
您只需编写代码即可,纯Java7。此代码保持新列表的平衡,并在最后添加额外的元素
public List<List<Integer>> resizeListOfNestedList(int newSize, List<List<Integer>> data) {
ArrayList<Integer> allElements = new ArrayList<>();
for (List<Integer> integers : data) {
allElements.addAll(integers);
}
int elementsPerItem = allElements.size() / newSize;
int extraElements = allElements.size() % newSize;
int indexToStartAddExtraElement = newSize - extraElements;
ArrayList<List<Integer>> result = new ArrayList<>(newSize);
Iterator<Integer> iterator = allElements.iterator();
for (int i = 0; i < newSize; i++){
int currentItemElementsCount = elementsPerItem;
if (i >= indexToStartAddExtraElement)
currentItemElementsCount++;
ArrayList<Integer> current = new ArrayList<>(currentItemElementsCount);
for (int j = 0; j < currentItemElementsCount; j++){
current.add(iterator.next());
}
result.add(current);
}
return result;
}
public List resizeListOfNestedList(int newSize,列表数据){
ArrayList等位基因=新的ArrayList();
对于(列出整数:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] // Incoming list
[[1, 2], [3, 4], [5, 6], [7, 8], [9]] // Final list
import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Lists.*;
public void test(){
List<Integer> a = newArrayList(1,2,3);
List<Integer> b = newArrayList(4,5,6);
List<Integer> c = newArrayList(7,8,9);
List<List<Integer>> incomingList = newArrayList(a,b,c);
System.out.println(incomingList);
// Repartition
List<List<Integer>> finalList = newArrayList(partition(concat(incomingList), 2));
System.out.println(finalList);
}
public List<List<Integer>> resizeListOfNestedList(int newSize, List<List<Integer>> data) {
ArrayList<Integer> allElements = new ArrayList<>();
for (List<Integer> integers : data) {
allElements.addAll(integers);
}
int elementsPerItem = allElements.size() / newSize;
int extraElements = allElements.size() % newSize;
int indexToStartAddExtraElement = newSize - extraElements;
ArrayList<List<Integer>> result = new ArrayList<>(newSize);
Iterator<Integer> iterator = allElements.iterator();
for (int i = 0; i < newSize; i++){
int currentItemElementsCount = elementsPerItem;
if (i >= indexToStartAddExtraElement)
currentItemElementsCount++;
ArrayList<Integer> current = new ArrayList<>(currentItemElementsCount);
for (int j = 0; j < currentItemElementsCount; j++){
current.add(iterator.next());
}
result.add(current);
}
return result;
}