如何合并两个不重复的 ArrayList? [英] How to merge two ArrayLists without duplicates?

查看:33
本文介绍了如何合并两个不重复的 ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组列表

ArrayList one = {A, B, C, D, E}
ArrayList two = {B, D, F, G}  

我想要我的最终 ArrayList,它包含 All 一的元素和只有两个而不是一个的元素.

I want to have my final ArrayList which will have All the elements of one and the elements which are only in two and not in one.

所以ArrayList final = {A、B、C、D、E、F、G}.

So ArrayList final = {A, B, C, D, E, F, G}.

我该怎么做?

推荐答案

for (Object x : two){
   if (!one.contains(x))
      one.add(x);
}

假设您不想使用评论中建议的集合.如果您正在寻找比这更有趣的东西,请澄清您的问题.

assuming you don't want to use the set suggested in the comment. If you are looking for something fancier than this please clarify your question.

这篇关于如何合并两个不重复的 ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆