在具有相同大小的两个Java流上进行迭代,并从一个流中覆盖另一个流的值。

huangapple go评论66阅读模式
英文:

iterating over two java streams with the same size and override values from one on another

问题

我有两个列表,它们的大小相同。我想同时迭代这两个列表,并将一个列表中的值覆盖到另一个列表中。

例如,我有两个列表 ListA 和 ListB,其中包含一些数据,它们可以通过 id 进行比较,我想要得到如下结果:

List<SomeObject> listA;
List<AnotherObject> listB;

listA: [
    [id=111;
    name='AAA';
    age=65],
    [id=222;
    name='BBB';
    age=11]
]

listB: [
    [id=111;
    name='';
    age=null],
    [id=222;
    name='';
    age=null]
]

result: [
    [id=111;
    name='AAA';
    age=65],
    [id=222;
    name='BBB';
    age=11]
]

它们当然还有一些其他不同的属性,应该保持原样。所以我只需要覆盖其中的两个属性。

英文:

I have two Lists and they have same size. I would like to iterate over both of them simultaneously and override values from one stream on another stream.

For example I have two lists ListA and ListB with some data and they can be compared by id and I would like to get as a result like below:

List&lt;SomeObject&gt; listA;
List&lt;AnotherObject&gt; listB;

listA: {
[id=111;
name=&#39;AAA&#39;;
age=65],
[id=222;
name=&#39;BBB&#39;;
age=11],
}

listB: {
[id=111;
name=&#39;&#39;;
age=null],
[id=222;
name=&#39;&#39;;
age=null],
}

result:{
[id=111;
name=&#39;AAA&#39;;
age=65],
[id=222;
name=&#39;BBB&#39;;
age=11],
}

They have some other properties of course which are different and should stay as it is. So I need to override just two of them

答案1

得分: 1

Sure, here's the translation:

  1. 从一个列表(按 id 分组)创建一个映射,例如从 'listB'。
  2. 使用流或循环遍历另一个列表(listA),对于每个项目,从创建的映射中找到相应的按 id 匹配的项目。
  3. 如果条目按 id 匹配,则替换所有值为 null 或空字符串的内容。
英文:
  1. Create a map from one list (grouping by id) for example from 'listB'
  2. Iterate by other list (listA) with stream or for-loop, and for every item find by id corresponding item from created map.
  3. If entries match by id replace all values that are null or empty string.

huangapple
  • 本文由 发表于 2020年8月20日 16:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63501578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定