如何确定集合中的所有元素是否具有与流IPA相同的值
我正在尝试搜索集合并根据集合中的每个项目是否都有一个返回true
或false
匹配 boolean
属性。
我做了一个简单的示例,其中“falseList”返回 false
。
所以我的问题是,是否有一个流调用可以干净地检查集合中匹配的 boolean
值,这样“falseList”将导致 true
因为所有属性都匹配?
我知道我可以获取集合中的第一项,然后使用 2
行中的 allMatch()
将所有值与第一项进行比较,但是我正在寻找出于好奇,单线。
class BooleanClass {
boolean val;
public BooleanClass(boolean val) {
this.val = val;
}
public boolean getVal() {
return this.val;
}
}
public class AllMatch {
public static void main(String[] args) {
BooleanClass a = new BooleanClass(true);
BooleanClass b = new BooleanClass(true);
BooleanClass c = new BooleanClass(true);
ArrayList<BooleanClass> trueList = new ArrayList<BooleanClass>();
trueList.add(a);
trueList.add(b);
trueList.add(c);
System.out.println(trueList.stream().allMatch(l -> l.getVal())); // true
BooleanClass d = new BooleanClass(false);
BooleanClass e = new BooleanClass(false);
BooleanClass f = new BooleanClass(false);
ArrayList<BooleanClass> falseList = new ArrayList<BooleanClass>();
falseList.add(d);
falseList.add(e);
falseList.add(f);
System.out.println(falseList.stream().allMatch(l -> l.getVal())); // false
}
}
当前输出:
true
false
期望输出:
true
true
I am trying to search a collection and return true
or false
based on if every item in the collection has a matching boolean
property.
I made a simple example where the 'falseList' returns false
.
So my question, is there a stream call to cleanly check for matching boolean
values in a collection, such that the 'falseList' will result in a true
since all properties match?
I'm aware that I can get the first item in the collection and then compare all values to the first item with allMatch()
in 2
lines, however I am looking for a one liner out of curiosity.
class BooleanClass {
boolean val;
public BooleanClass(boolean val) {
this.val = val;
}
public boolean getVal() {
return this.val;
}
}
public class AllMatch {
public static void main(String[] args) {
BooleanClass a = new BooleanClass(true);
BooleanClass b = new BooleanClass(true);
BooleanClass c = new BooleanClass(true);
ArrayList<BooleanClass> trueList = new ArrayList<BooleanClass>();
trueList.add(a);
trueList.add(b);
trueList.add(c);
System.out.println(trueList.stream().allMatch(l -> l.getVal())); // true
BooleanClass d = new BooleanClass(false);
BooleanClass e = new BooleanClass(false);
BooleanClass f = new BooleanClass(false);
ArrayList<BooleanClass> falseList = new ArrayList<BooleanClass>();
falseList.add(d);
falseList.add(e);
falseList.add(f);
System.out.println(falseList.stream().allMatch(l -> l.getVal())); // false
}
}
Current output:
true
false
Desired output:
true
true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您需要检查 list 中的所有元素是否具有相同的值(全部
true
或全部false
)。您希望在单个流语句中实现这一点。您可以使用
anyMatch()
来完成此操作。使用此操作进行检查相对于allMatch()
的优势在于anyMatch()
是一个短路手术。即当它遇到与给定谓词不匹配的元素时,它会终止流管道的执行并返回结果。它可能看起来像这样:
方法
isNonUniform()
顾名思义,以及如何从传递到anyMatch()
的 谓词 中看到,检查给定集合是否至少包含一个与其他集合不同的元素。注意,访问列表的第一个元素安全,因为如果列表为空,则谓词将不会被评估。
另请注意,当流中没有元素时,
anyMatch()
将返回false
。即空列表被认为是统一的(不是非统一的)。如果您想在列表为空时获取true
,请将此条件附加在流前面:falseList.isEmpty() ||
。从技术上讲,它仍然是一句俏话。因此,为了确定列表是否统一,应该这样使用:
旁注:我个人认为有
not
或non
在方法名称中(这并不完美)从干净编码的角度来看是较小的邪恶,然后将逻辑非!
放在流前面,这使得它变得更难阅读。So you need to check whether all elements in a list have the same value (either all
true
or allfalse
). And you want to achieve that in a single stream statement.You can do it with
anyMatch()
. The upper hand of checking with this operation overallMatch()
is thatanyMatch()
is a short-circuit operation. I.e. when it encounters the element that doesn't match the given predicate, it terminates the execution of the stream pipe-line and return the result.That how it might look like:
Method
isNonUniform()
as its name suggests and how you can see from the predicate passed intoanyMatch()
, checks whether the given collection contains at least one element that is different from the others.Note, that accessing the first element of the list is safe because if the list is empty, the predicate will not be evaluated.
Also note that
anyMatch()
will returnfalse
when there are no elements in the stream. I.e. an empty list is considered to be uniform (not nonUniform). In case if you want to obtaintrue
when the list is empty, then append this condition in front of the stream:falseList.isEmpty() ||
. Technically, it still will be a one-liner.So in order to determine if the list is uniform, it should be used this way:
Side-note: I personally think that having
not
ornon
in the method name (which is not perfect) from the clean-coding perspective is smaller evil, then placing logical not!
in front of the stream, which makes it harder to read.首先,您的问题提到了
allMatch
,但是,您在代码中使用了anyMatch
。无论哪种方式,对于计算结果为true
的内容,lambda 计算结果应为true
。在你的情况下,第一个表达式l ->; l.getVal()
的计算结果始终为true
。对于第二个表达式l -> l.getVal()
的计算结果始终为false
,因此有效结果将为false
。要使其为true
,您必须将 lambda 更改为l -> l.getVal() == false
。不确定这是否是您想要实现的目标。Firstly, your question mentions
allMatch
, however, you are usinganyMatch
in your code. Either way, for something to evaluate totrue
, the lambda should evaluate totrue
. In your case, the first expressionl -> l.getVal()
always evaluates totrue
. For second expressionl -> l.getVal()
always evaluates tofalse
, hence the effective result will befalse
. To make ittrue
, you have to change the lambda tol -> l.getVal() == false
. Not sure if that's what you want to achieve.