从Java中的对象列表中通过与另一个数组进行比较来移除元素。

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

Remove elements from List of objects by comparing with another array in Java

问题

我有一个订阅列表

subscriptions = [
{
code: "Heloo",
value: "一些值",
reason: "一些原因"
},
{
code: "Byeee",
value: "一些再见",
reason: "一些再见"
},
{
code: "World",
value: "一些世界",
reason: "一些世界"
}
]

我还有一个取消订阅列表:

unsubscribe: ["Heloo", "World"]

我想通过比较这两个数组来取消订阅订阅列表中的元素

最终结果:

subscriptions = [
{
code: "Byeee",
value: "一些再见值",
reason: "一些再见原因"
}
]

以下是我的解决方案:

List newList = new ArrayList<>();
for (String products : subscriptions) {
newList.add(products.code);
}

if (!newList.containsAll(unsubscribe)) {
log.error("有些产品未订阅");
}

for (int i = 0; i < subscriptions.size(); i++) {
if(unsubscribe.contains(subscriptions.get(i).code)) {
subscriptions.remove(i);
}
}

这个方法可能还有优化的空间,我正在寻找一个更好/更优化的解决方案。

英文:

I have a list of subscriptions

subscriptions = [
      { 
        code : &quot;Heloo&quot;,
        value:&quot;some value&quot;,
        reason : &quot;some reason&quot;
      },
      { 
        code : &quot;Byeee&quot;,
        value:&quot;some byee&quot;,
        reason : &quot;some byee&quot;
      },
      { 
        code : &quot;World&quot;,
        value:&quot;some World&quot;,
        reason : &quot;some world&quot;
      }
    ]

I have another list of unsubscriptions:

unsubscribe : [&quot;Heloo&quot;,&quot;World&quot;]

I want to unsubscribe elements in the subscriptions by comparing these two arrays

Final Result :

subscriptions = [
  { 
    code : &quot;Byeee&quot;,
    value:&quot;some byee value&quot;,
    reason : &quot;some byee reason&quot;
  }
]

Below is my solution :

List&lt;String&gt; newList = new ArrayList&lt;&gt;();
for (String products : subscriptions) {
        newList.add(products.code);
 }

if (!newList.containsAll(unsubscribe) {
        log.error(&quot;Few products are not subscribed&quot;);  
}

for (int i = 0; i &lt; subscriptions.size(); i++) {
        if(unsubscribe.contains(subscriptions.get(i).code)) {
          subscriptions.remove(i);
        }
}

This could be better . I am looking for a better/optimized solution.

答案1

得分: 3

使用removeIf会显著简化您的代码:

List<Subscription> subscriptions =  ... ;
List<String> unsubscribe = ...;

subscriptions.removeIf(s -> unsubscribe.contains(s.code));
英文:

Using removeIf will clean up your code considerably:

List&lt;Subscription&gt; subscriptions =  ... ;
List&lt;String&gt; unsubscribe = ...;

subscriptions.removeIf(s -&gt; unsubscribe.contains(s.code));

答案2

得分: 0

你也可以使用流来完成:

List<String> newList = subscriptions
                            .stream()
                            .filter(it -> !unsubscribe.contains(it.code))
                            .collect(Collectors.toList());
英文:

You can also do it with streams:

List&lt;String&gt; newList = subscriptions
                                 .stream()
                                 .filter(it -&gt; !unsubscribe.contains(it.code))
                                 .collect(Collectors.toList());

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

发表评论

匿名网友

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

确定