简体   繁体   中英

Why does allMatch return true in case of empty list in Java?

List<String> strings = new ArrayList<>();
boolean doAllMatch = strings.stream().allMatch(str -> str.startsWith("a"));

How funny is this. It is an empty list. Still it says all match. Is this what we as developer really want ??

Yes, generally. That's how pretty much every definition of all in any language works -- it's true that all the elements match the condition. There is no element that doesn't match the condition, after all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM