英文:
Merge elements of array based on index position or id
问题
我有一个来自数据库的列表数组。我想创建一个新的列表,其中包含按照它们的索引位置或id分组的数组中的所有元素。
以下是我正在处理的数组:
[
{
"content": "Content A1",
"id": 1
},
{
"content": "Content A2",
"id": 2
},
{
"content": "Content A3",
"id": 3
},
{
"content": "Content A4",
"id": 4
},
{
"content": "Content A5",
"id": 5
}
]
[
{
"content": "Content B1",
"id": 1
},
{
"content": "Content B2",
"id": 2
},
{
"content": "Content B3",
"id": 3
},
{
"content": "Content B4",
"id": 4
},
{
"content": "Content B5",
"id": 5
}
]
[
{
"content": "Content C1",
"id": 1
},
{
"content": "Content C2",
"id": 2
},
{
"content": "Content C3",
"id": 3
},
{
"content": "Content C4",
"id": 4
},
{
"content": "Content C5",
"id": 5
}
]
这是我希望新的列表数组看起来的样子:
[
{
"content": ["Content A1","Content B1","Content C1"],
"id": 1
},
{
"content": ["Content A2", "Content B2", "Content C2"],
"id": 2
},
{
"content": ["Content A3","Content B3","Content C3"],
"id": 3
},
{
"content": ["Content A4","Content B4","Content C4"],
"id": 4
},
{
"content": ["Content A5","Content B5","Content C5"],
"id": 5
}
]
我尝试了以下代码,但它只返回了最后一个对象:
List<String> contentArray = new ArrayList<>();
for (int i = 0; i < b.listDetail.size(); i++) {
int finalI = i;
contentArray = listResource.stream()
.map(a -> a.listDetail.get(finalI).content)
.collect(Collectors.toList());
}
System.out.println(contentArray);
英文:
I have a list array which I'm getting from the database. I want to create a new List with all elements in the array grouped by their index position or id.
This is the array I'm working with:
[
{
"content": "Content A1",
"id": 1
},
{
"content": "Content A2",
"id": 2
},
{
"content": "Content A3",
"id": 3
},
{
"content": "Content A4",
"id": 4
},
{
"content": "Content A5",
"id": 5
}
]
[
{
"content": "Content B1",
"id": 1
},
{
"content": "Content B2",
"id": 2
},
{
"content": "Content B3",
"id": 3
},
{
"content": "Content B4",
"id": 4
},
{
"content": "Content B5",
"id": 5
}
]
[
{
"content": "Content C1",
"id": 1
},
{
"content": "Content C2",
"id": 2
},
{
"content": "Content C3",
"id": 3
},
{
"content": "Content C4",
"id": 4
},
{
"content": "Content C5",
"id": 5
}
]
This is how I want the new List array to look like:
[
{
"content": {"Content A1","Content B1","Content C1"}
"id": 1
},
{
"content": {"Content A2", "Content B2", "Content C2"}
"id": 2
},
{
"content": {"Content A3","Content B3","Content C3"}
"id": 3
},
{
"content": {"Content A4","Content B4","Content C4"}
"id": 4
},
{
"content": {"Content A5","Content B5","Content C5"}
"id": 5
}
]
I have tried this code but it returns only the last object:
List<String> contentArray = new ArrayList<>();
for (int i = 0; i < b.listDetail.size(); i++) {
int finalI = i;
contentArray = listResource.stream()
.map(a -> a.listDetail.get(finalI).content)
.collect(Collectors.toList());
}
System.out.println(contentArray);
答案1
得分: 1
你已经快完成了,只需要将contentArray存储在一个列表中,适用于所有索引。
List<List<String>> res = new ArrayList<>();
for (int i = 0; i < b.listDetail.size(); i++) {
int finalI = i;
List<String> contentArray = listResource.stream()
.map(a -> a.listDetail.get(finalI).content)
.collect(Collectors.toList());
res.add(contentArray); // 将一个索引下的所有内容存储在列表中
}
你可以为你的响应创建一个带有id
和contents
的类,在循环内部构造该类并添加到列表中。
英文:
You are almost done, just need to store contentArray in a list for all index.
List<List<String>> res = new ArrayList<>();
for (int i = 0; i < b.listDetail.size(); i++) {
int finalI = i;
List<String> contentArray = listResource.stream()
.map(a -> a.listDetail.get(finalI).content)
.collect(Collectors.toList());
res.add(contentArray); // store all contents for a index in list
}
You can create a class for your response with id
and contents
and construct inside loop and add in the list.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论