Java8 lambda移除List<?>中的元素并更新bool型标志值。

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

Java8 lambda remove element from List<?> and update the boolean flag value

问题

我需要在从List&lt;String&gt;中移除某个对象时,设置一个标志`nameRemoved=true`。

这是我目前使用的传统方法。

List<String> list = new ArrayList<String>();
if (list.contains("abc")) {
list.remove("abc");
nameRemoved = true;
}


我可以使用以下方法从列表中删除元素,但如何使用lambda语法设置标志值为`nameRemoved=true`呢?

List<String> list = new ArrayList<String>();
list.removeIf(name -> name.equalsIgnoreCase("abc"));

英文:

I've to set one flag nameRemoved=true, when I remove some object from the List&lt;String&gt;

This what traditional approach I'm using here.

List&lt;String&gt; list = new ArrayList&lt;String&gt;();
if (list.contains(&quot;abc&quot;)) {
    list.remove(&quot;abc&quot;);
    nameRemoved=true
}

I can remove element from the list using below but how can I also set the flag value to nameRemoved=true using lambda syntaxes ?

List&lt;String&gt; list = new ArrayList&lt;String&gt;();
list.removeIf(name -&gt; name.equalsIgnoreCase(&quot;abc&quot;));

答案1

得分: 7

根据op的要求,我把我的评论作为一个回答。

因为removeIf在任何元素被移除时会返回true,你可以根据结果设置你的标志。

nameRemoved = list.removeIf(name -> name.equalsIgnoreCase("abc"))

或者更安全并且使用方法引用:

nameRemoved = list.removeIf("abc"::equalsIgnoreCase);

编辑:
正如@Holger评论的那样,这个答案只关注如何用removeIf替换代码。但这段代码与第一个代码有很大不同。第一个代码只会移除列表中与给定字符串完全匹配的一个元素。而我提供的代码会移除所有忽略大小写匹配的元素。

英文:

As op requested I put my comment as an answer.
Because removeIf return true if any element was removed you can set your flag based on the result.

nameRemoved = list.removeIf(name -&gt; name.equalsIgnoreCase(&quot;abc&quot;))

Or safer and with a method reference:

nameRemoved = list.removeIf(&quot;abc&quot;::equalsIgnoreCase);

EDIT:
As @Holger commented this answer only focus on how to replace the code with the removeIf. But this code is quite different from the first one. The first one remove only one element from the list that match exactly the given string. The one I provided will remove all element that match ignoring the case.

答案2

得分: 3

RemoveIf 返回一个布尔值,因此您可以这样做:

List<String> list = new ArrayList<String>();
boolean nameRemoved = list.removeIf(name -> "abc".equalsIgnoreCase(name));

或者

List<String> list = new ArrayList<String>();
boolean nameRemoved = list.removeIf("abc"::equalsIgnoreCase);

链接:https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeIf-java.util.function.Predicate-

英文:

RemoveIf returns a boolean so you can do this:

List&lt;String&gt; list = new ArrayList&lt;String&gt;();
boolean nameRemoved = list.removeIf(name -&gt; &quot;abc&quot;.equalsIgnoreCase(name));

or

List&lt;String&gt; list = new ArrayList&lt;String&gt;();
boolean nameRemoved = list.removeIf(&quot;abc&quot;::equalsIgnoreCase);

https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeIf-java.util.function.Predicate-

huangapple
  • 本文由 发表于 2020年9月24日 23:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64049327.html
匿名

发表评论

匿名网友

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

确定