当前位置: 首页 > 知识库问答 >
问题:

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

谭骏
2023-03-14

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

My firstList

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

编辑!

实际上我的两个列表都是数组列表,所以我的列表也是

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

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

共有3个答案

羊时铭
2023-03-14

如果这些是数组列表

list1.addAll(list2);

它应该适用于您想要的。

艾心远
2023-03-14

我不太确定你有一个<code>ArrayList</code>(来自输出),但即使如此,如果它是一个实现<code>Collection</code’接口的类,你可以使用<code>addAll</code〕方法,而不依赖于确切的类(只要对象是同一类型的)。

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

秦信瑞
2023-03-14

鉴于:

   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());
 类似资料:
  • 问题内容: 我有 我想要 问题答案:

  • 问题内容: 我有一个带有20个属性的Java类及其相应的getter和setters。我也有两个对象列表:和。 现在,我想合并两个列表,并避免基于和的重复对象。 使用Java 8: 但是我必须在哪里指定属性?我应该重写和方法吗? 问题答案: 如果要实现and ,则 在 类 内部 进行操作。在该类中添加类似的方法 如果您这样做了,在上调用将自动执行正确的操作。 如果您不想(或无法)更改类,则没有平等

  • 问题内容: 我厌倦了总是不得不编写这样的代码: 或者,如果我不想自己编写代码,请实现一个已经执行过的库。ES6 +肯定会在这方面获救,这将为我们提供类似a 或 那么ES6 +是否提供这种功能?如果尚不存在,那么是否计划了此类功能?如果没有计划,那为什么不呢? 问题答案: 您将可以通过使用Object.assign在ES6中进行浅合并/扩展/分配: 句法: Object.assign( target

  • 我有一个Java类,它有20个属性及其相应的getter和setter。还有两个对象列表:和。 使用Java 8: 但是我必须在哪个地方指定属性呢?是否应该重写和方法?

  • 问题内容: 例如,如果我有一个元组列表 如何解开元组并将其重新格式化为一个列表 我认为这也与功能有关,但是我真的不知道该怎么做。请赐教。 问题答案: b = [i for sub in a for i in sub] 这样就可以了。

  • 问题内容: 我正在写一个小脚本来帮助日本假名记忆。我如何将以下列表合并为一个?我尝试如下。 问题答案: 单程:

  • 问题内容: 我试图在我的类中有一个默认对象,它具有可接受的默认属性,并让开发人员通过指定另一个对象来覆盖其中的一些默认属性,但是我找不到一种很好的方法来做到这一点。 预期的用法如下: 问题答案: 实现该接口,因此您 可以 按原样对待它,并使用诸如添加another的内容之类的方法。 但是,如果您将其视为地图,则需要非常小心: 因为它 看起来 像一个复制构造函数,但事实并非如此,这常常使人们感到 困

  • 在这里,我有两个项目列表,我想要合并到一个列表中,并在保存到数据库之前删除重复项。但是我得到一个错误,“非静态方法不能从静态上下文引用”。虽然我知道该消息的含义,但我不知道如何在Java8流的上下文中解决它。拜托,救命。 下面是错误消息的快照