gpt4 book ai didi

java - 如何将两个 List 合并为一个 List

转载 作者:行者123 更新时间:2023-12-01 13:15:11 24 4
gpt4 key购买 nike

我看到了其他一些关于同一件事的问题,但其中任何一个都解决了我的问题。可能是因为我不明白它的概念。

我有两个List<String> :

List<String> PlatformInfo1 = new ArrayList
List<String> PlatformInfo2 = new ArrayList

这些列表内容包含我数据库中的一些信息:

PlatformInfo1= [{"Nnodes":"163"}]
PlatformInfo2= [{"Commnet":"Windows Machine"}]

为了与每个元素交互,我尝试将两个数组包含在一个对象中 List<String> :

List<String> PlatformInfoGI = new ArrayList<String>();
PlatformInfoGI.addAll(PlatformInfo1);
PlatformInfoGI.addAll(PlatformInfo2);

我得到的结果是:

Value of PlatformInfoGI: [{"Nnodes":"163"},{"Commnet":"Windows Machine"}]

可以采取一些措施来改变这些 List<String>变成这样的东西:

 Value of PlatformInfoGI: [{"Nnodes":"163","Commnet":"Windows Machine"}]

NOTE元素之间缺少大括号。

我想要得到的是 List<String>具有一个元素和不同的属性(字符串)

最佳答案

使用 apache commons 集合'ListUtils.union(java.util.List list1, java.util.List list2)

Returns a new list containing the second list appended to the first list. The List.addAll(Collection) operation is used to append the two given lists into a new list.

关于java - 如何将两个 List<String> 合并为一个 List<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com