java 如何在java中将两个对象列表合并为一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11414083/
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
How to join two list of objects into one in java
提问by user359187
Hi i have two ArrayList of objects and i need to merge it as a single list. Here is my requirement
嗨,我有两个 ArrayList 对象,我需要将它合并为一个列表。这是我的要求
My firstList
我的第一个列表
ListA
列表A
{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl}
and listB
和列表B
{sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, `thirdmonth=30}`
and i need the result to be
我需要结果是
Result
结果
{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl, sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, thirdmonth=30}
EDIT!
编辑!
Actually my Both list are arrayList so my listA will be
实际上我的两个列表都是 arrayList 所以我的 listA 将是
{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl}
{StaffFirstName=demo35, resourceId=3, totalcost=19625.0, totalPercentageInvolvment=785, ResourceCost=2500, staffRole=sweeper}
and list B will be
和列表 B 将是
{sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90}
{sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90}
and the result should be
结果应该是
{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl, sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, thirdmonth=30}
{StaffFirstName=demo35, resourceId=3, totalcost=19625.0, totalPercentageInvolvment=785, ResourceCost=2500, staffRole=sweeper, sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90}
That means each row of my tow list must be append my row wise. If i use addAll
function the two list just append like this
这意味着我的拖车列表的每一行都必须明智地附加我的行。如果我使用addAll
函数,两个列表就这样附加
{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl}
{StaffFirstName=demo35, resourceId=3, totalcost=19625.0, totalPercentageInvolvment=785, ResourceCost=2500, staffRole=sweeper}
{sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, thirdmonth=30}
{sixthmonth=100, fourthmonth=30, firstmonth=40, fifthmonth=25, secondmonth=100, thirdmonth=90}. But i need to append the two list row wise. Is it possible?
回答by Andy Thomas
Given:
鉴于:
List<MyClass> listA, listB;
Try this:
试试这个:
List<MyClass> union = new ArrayList<MyClass>();
union.addAll( listA );
union.addAll( listB );
回答by pcalcao
I'm not quite sure that what you have is an ArrayList
(from the output), but even so, if it's a class that implements the Collection
interface, you can use the addAll
method, independent of the exact class (as long as the objects are of the same type).
我不太确定你所拥有的是一个ArrayList
(来自输出),但即便如此,如果它是一个实现Collection
接口的类,你可以使用该addAll
方法,独立于确切的类(只要对象是同类型)。
http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#addAll(java.util.Collection, T...)
http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#addAll(java.util.Collection, T...)
回答by ametren
If these are ArrayList<> objects containing the same type, you can use this:
如果这些是包含相同类型的 ArrayList<> 对象,则可以使用:
list1.addAll(list2);
and it should work fine for what you want.
它应该可以很好地满足您的需求。
回答by NIlesh Sharma
Type is the type of array....
类型是数组的类型....
Type[] concat(Type[] listA, Type[] listB)
{
Type[] list= new Type[listA.length+listB.length];
System.arraycopy(listA, 0, list, 0, listA.length);
System.arraycopy(listB, 0, list, listA.length, listB.length);
return list;
}