java 两个不同的ArrayList合并?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16102331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Two different ArrayList merge?
提问by Akarsh M
private ArrayList<SublistLocalData> catList;
private ArrayList<Localdata> nameList ,nameList1;
Both having the different-different data:
两者都有不同的数据:
for(SublistLocalData list: catList )
nameList1.addAll( list.nameList );
I am trying this concept for add the one type arrayList data into a different type ArrayList, I follow this link:
我正在尝试使用这个概念将一种类型的 arrayList 数据添加到不同类型的 ArrayList 中,我按照这个链接:
What should i do to solve this ?
我该怎么做才能解决这个问题?
回答by Damian Leszczyński - Vash
Question What should i do to solve this ?
问题 What should i do to solve this ?
Answer: Find a tutorial about Collection frameworkin Java and read it.
回答:找一个关于Java集合框架的教程并阅读它。
If you want to store objects of different type in single list, you should create a list that can accept any object. Every object in Java inherit from Object
class. Therefore you need to create something like:
如果要将不同类型的对象存储在单个列表中,则应创建一个可以接受任何对象的列表。Java 中的每个对象都继承自 Object
类。因此,您需要创建如下内容:
private List<Object> list4Everything = new ArrayList<>();
EDIT:
编辑:
How ever if you have same object and you just want to merge two already existing list into new single one. You should use the method List#addAll(Collection c)
如果您有相同的对象并且只想将两个已经存在的列表合并为一个新的列表。您应该使用List#addAll(Collection c) 方法
Lest do it on example, we will have three lists:
以免在示例中这样做,我们将有三个列表:
private List<YourType> list1 = new ArrayList<>();
private List<YourType> list2 = new ArrayList<>();
private List<YourType> mergeResult = new ArrayList<>();
You have various option to insert all data from list1
and 'list2' into mergeResult
您有多种选项可以将所有数据从list1
'list2' 插入mergeResult
Option 1 - Using addAll of megeResult.
选项 1 - 使用 megeResult 的 addAll。
mergeResult.addAll(list1);
mergeResult.addAll(list2);
Option 2 - Do it by hand.
选项 2 - 手动完成。
for(YourType yt : list1) { //Itereate throug every item from list1
mergerResult.add(yt); //Add found item to mergerResult
}
for(YourType yt : list1) { //Itereate throug every item from list2
mergerResult.add(yt); //Add found item to mergerResult
}
Note: In this solution you have possibility to select witch item is added to meregeResult
.
注意:在此解决方案中,您可以选择将女巫项目添加到meregeResult
.
So for example if we would like to have distinct result we could do something like this.
例如,如果我们想要不同的结果,我们可以做这样的事情。
for(YourType yt : list1) { //Itereate throug every item from list1
if(mergerResult.contains(yt) == false) { //We check that item, already exits in mergeResult
mergerResult.add(yt); //Add found item to mergerResult
}
}
for(YourType yt : list1) { //Itereate throug every item from list2
if(mergerResult.contains(yt) == false) { //We check that item, already exits in mergeResult
mergerResult.add(yt); //Add found item to mergerResult
}
}
As we perform twice the same operation we could create a utilities class.
当我们执行两次相同的操作时,我们可以创建一个实用程序类。
public static <T> boolean addAllNotContained(Collection<? super T> traget, Collection<? super T> source) {
//We should assure first that target or source are not null
int targetSize = target.size();
for(T item: source) {
if(target.contains(item) == false) {
target.add(item);
}
}
return targetSize != target.size();
}
Note: To have similar result of distinct merge you could use jest Set. It is created to not store duplicates.
注意:要获得类似的不同合并结果,您可以使用 jest Set。创建它是为了不存储重复项。
The another option is too use already created framework that extend default Java one. The options are guava with iterablesand apache commons
另一种选择是使用已经创建的扩展默认 Java 框架的框架。选项是guava with iterables和apache commons
回答by javadev
If there are any realtionships between SublistLocalData class and the Localdata class you can create a class that is a super class both of them or an Interface which the two classes implements.
Say you have this class : class AbstractSuperClass {}
Then : Here how to declare a list of the superClass : List<AbstractSuperClass> list;
Using this list, you can add any type of AbstractSuperClass (classes that inherits from it like SublistLocalData and Localdata )
You can then write :
如果 SublistLocalData 类和 Localdata 类之间存在任何关系,您可以创建一个类,该类既是它们的超类,也可以是这两个类实现的接口。假设你有这个类:class AbstractSuperClass {}
那么:这里如何声明 superClass 的列表:List<AbstractSuperClass> list;
使用这个列表,你可以添加任何类型的 AbstractSuperClass(继承自它的类,如 SublistLocalData 和 Localdata)然后你可以写:
list.add(new SublistLocalData());
orlist.add(new Localdata());