我想用Stream.allMatch()
,但当流空的时候我需要false
.
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(testMethod(Stream.empty())); // <- expected false (but is true)
System.out.println(testMethod(Stream.of("match", "match"))); // <- expected true
System.out.println(testMethod(Stream.of("match", "no match"))); // <- expected false
}
private static boolean testMethod(Stream<String> stream) {
return stream.allMatch(text -> "match".equals(text));
}
我不想用...
-
.toList()
,因为我想要How does short-circuit work for Java 8 Streams? -
.reduce()
,因为How does short-circuit work for Java 8 Streams?
我想,我不得不用noMatch()
,但我没有得到正确的否定.
private static boolean testMethod(Stream<String> stream) {
// my guess, but the results are wrong
return !stream.noneMatch(text -> !"match".equals(text));
}
这不是How to return false for an empty list if using Stream.allMatch()?的副本,因为我使用的是Stream
而不是List
.
也许是X-Y问题,所以这是我的上层用例
It's about status-collecting. Let's simply this as a download-status indicator for the user. (Not my real use-case, but it is close).
I have a table downloads
which looks like this
userId | download_name | download_status | data |
---|---|---|---|
1 | download 1 | loading | null |
2 | download 2 | ready | 0x4545475 |
I can't change the tables and I'm limited because it is a huge project (sadly not possible to refactor the world).
Anyways, I want to show the user an indicator about his downloads. The indicator should be only visible, if all downloads are ready. But, if there no downloads yet, then the user should not see the indicator.
我有一个给定的存储库-方法Stream<Downloads> getDownloadsForUser()
.短路是降低负荷的重要环节.