合并数组元素基于索引位置或者ID

huangapple go评论79阅读模式
英文:

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:

[
    {
        &quot;content&quot;: &quot;Content A1&quot;,
        &quot;id&quot;: 1
    },
    {
        &quot;content&quot;: &quot;Content A2&quot;,
        &quot;id&quot;: 2
    },
    {
        &quot;content&quot;: &quot;Content A3&quot;,
        &quot;id&quot;: 3
    },
    {
        &quot;content&quot;: &quot;Content A4&quot;,
        &quot;id&quot;: 4
    },
    {
        &quot;content&quot;: &quot;Content A5&quot;,
        &quot;id&quot;: 5
    }
]

[
    {
        &quot;content&quot;: &quot;Content B1&quot;,
        &quot;id&quot;: 1
    },
    {
        &quot;content&quot;: &quot;Content B2&quot;,
        &quot;id&quot;: 2
    },
    {
        &quot;content&quot;: &quot;Content B3&quot;,
        &quot;id&quot;: 3
    },
    {
        &quot;content&quot;: &quot;Content B4&quot;,
        &quot;id&quot;: 4
    },
    {
        &quot;content&quot;: &quot;Content B5&quot;,
        &quot;id&quot;: 5
    }
]

[
    {
        &quot;content&quot;: &quot;Content C1&quot;,
        &quot;id&quot;: 1
    },
    {
        &quot;content&quot;: &quot;Content C2&quot;,
        &quot;id&quot;: 2
    },
    {
        &quot;content&quot;: &quot;Content C3&quot;,
        &quot;id&quot;: 3
    },
    {
        &quot;content&quot;: &quot;Content C4&quot;,
        &quot;id&quot;: 4
    },
    {
        &quot;content&quot;: &quot;Content C5&quot;,
        &quot;id&quot;: 5
    }
]

This is how I want the new List array to look like:

[
{
     &quot;content&quot;: {&quot;Content A1&quot;,&quot;Content B1&quot;,&quot;Content C1&quot;}
     &quot;id&quot;: 1
},
{
     &quot;content&quot;: {&quot;Content A2&quot;, &quot;Content B2&quot;, &quot;Content C2&quot;}
     &quot;id&quot;: 2
},
{
     &quot;content&quot;: {&quot;Content A3&quot;,&quot;Content B3&quot;,&quot;Content C3&quot;}
     &quot;id&quot;: 3
},
{
     &quot;content&quot;: {&quot;Content A4&quot;,&quot;Content B4&quot;,&quot;Content C4&quot;}
     &quot;id&quot;: 4
},
{
     &quot;content&quot;: {&quot;Content A5&quot;,&quot;Content B5&quot;,&quot;Content C5&quot;}
     &quot;id&quot;: 5
}
]

I have tried this code but it returns only the last object:

  List&lt;String&gt; contentArray = new ArrayList&lt;&gt;();
  for (int i = 0; i &lt; b.listDetail.size(); i++) {
                int finalI = i;
                contentArray = listResource.stream()
                        .map(a -&gt; 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); // 将一个索引下的所有内容存储在列表中
}

你可以为你的响应创建一个带有idcontents的类,在循环内部构造该类并添加到列表中。

英文:

You are almost done, just need to store contentArray in a list for all index.

  List&lt;List&lt;String&gt;&gt; res = new ArrayList&lt;&gt;();
  for (int i = 0; i &lt; b.listDetail.size(); i++) {
       int finalI = i;
       List&lt;String&gt; contentArray = listResource.stream()
                        .map(a -&gt; 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.

huangapple
  • 本文由 发表于 2020年9月12日 03:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63853373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定