两种不同的ArrayList合并? [英] Two different ArrayList merge?

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

问题描述

private ArrayList<SublistLocalData> catList;

private ArrayList<Localdata> nameList ,nameList1;

两种具有不同-不同的数据:

Both having the different-different data:

for(SublistLocalData  list: catList )
nameList1.addAll( list.nameList );

我想这个概念一个ArrayList类型数据添加到不同类型的ArrayList,我请点击此链接:

I am trying this concept for add the one type arrayList data into a different type ArrayList, I follow this link:

链接

我应该怎么做才能解决这个问题?

What should i do to solve this ?

推荐答案

问题我应该怎么做才能解决这个问题?

答:查找关于Java中收集框架教程和阅读。

Answer: Find a tutorial about Collection framework in 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:

private List<Object> list4Everything = new ArrayList<>();

编辑:

如何过,如果你有相同的对象,你只想合并两个现有列表进入新的一个。您应该使用该方法的#列出的addAll(集三)

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)

免得做到这一点的例子中,我们将有三个列表:

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<>();

您有不同的选项,从 list1的插入的所有数据和列表2到 mergeResult

You have various option to insert all data from list1 and 'list2' into mergeResult

选项1 - 使用megeResult的的addAll

Option 1 - Using addAll of megeResult.

mergeResult.addAll(list1);
mergeResult.addAll(list2);

选项2 - 手工做吧

Option 2 - Do it by hand.

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
}

请注意:在这个解决方案有可能选择女巫项添加到 meregeResult

Note: In this solution you have possibility to select witch item is added to 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.

另一个选项也使用已经创建的框架,扩展默认的Java之一。选项​​有<一个href=\"http://google-collections.google$c$c.com/svn/trunk/javadoc/index.html?http://google-collections.google$c$c.com/svn/trunk/javadoc/com/google/common/collect/package-summary.html\"相对=nofollow>番石榴iterables 和阿帕奇公地

The another option is too use already created framework that extend default Java one. The options are guava with iterables and apache commons

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

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