gpt4 book ai didi

java - 如何将两个列表字符串组合成一个列表

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:39 24 4
gpt4 key购买 nike

我有两个不同的列表字符串,

List<String> A= [1,2,3,4]; 
List<String> B= [1,2,5,6];

我想合并两个列表,在新的列表字符串中 List C = new Arraylist();

如何合并两个列表字符串,如示例:

    C = [1,2,3,4,5,6];

最佳答案

使用Collection.addAll() , 和一个 TreeSet , 以删除重复项并保持结果排序。

Set<String> c = new TreeSet<String>(a); //create a Set with all the elements in a
c.addAll(b); //add all the elements in b

关于java - 如何将两个列表字符串组合成一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019751/

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