Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join two list of objects into one in java

Tags:

java

Hi i have two ArrayList of objects and i need to merge it as a single list. Here is my requirement

My firstList

ListA

{StaffFirstName=f2, resourceId=2, totalcost=18055.0, totalPercentageInvolvment=550, ResourceCost=2300, staffRole=tl}

and listB

{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

{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

{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

{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?
like image 254
user359187 Avatar asked Mar 06 '26 08:03

user359187


2 Answers

Given:

   List<MyClass> listA, listB;

Try this:

   List<MyClass> union = new ArrayList<MyClass>();
   union.addAll( listA );
   union.addAll( listB );

EDIT (Java 8 or later)

In Java 8 or later, you can use streams to join the lists in a single expression.

List<MyClass> union = Stream.concat( listA.stream(), listB.stream())
            .collect( Collectors.toList());
like image 78
Andy Thomas Avatar answered Mar 07 '26 23:03

Andy Thomas


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).

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#addAll(java.util.Collection, T...)

like image 32
pcalcao Avatar answered Mar 07 '26 21:03

pcalcao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!