gpt4 book ai didi

java - 如何在java中将两个对象列表合并为一个

转载 作者:行者123 更新时间:2023-12-01 06:46:22 25 4
gpt4 key购买 nike

嗨,我有两个对象的 ArrayList,我需要将其合并为一个列表。这是我的要求

我的第一个列表

列表A

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

和列表B

{sixthmonth=60, fourthmonth=40, firstmonth=10, fifthmonth=50, secondmonth=20, `thirdmonth=30}`

我需要结果是

结果

{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}

编辑!

实际上我的两个列表都是 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}

列表 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}

结果应该是

{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}

这意味着我的拖车列表的每一行都必须按行追加。如果我使用 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?

最佳答案

给定:

   List<MyClass> listA, listB;

试试这个:

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

编辑(Java 8 或更高版本)

在 Java 8 或更高版本中,您可以使用流在单个表达式中连接列表。

List<MyClass> union = Stream.concat( listA.stream(), listB.stream())
.collect( Collectors.toList());

关于java - 如何在java中将两个对象列表合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11414083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com