java - 检查多个变量是否为空的更短方法
问题描述
我有三个Integer
变量,不允许我更改为原始变量,int
我需要检查其中至少一个的值是否大于 0。下面是否有更短/更简洁的方法来重写我的代码:
Integer foo = // null or some value
Integer bar = // null or some value
Integer baz = // null or some value
boolean atLeastOnePositive = (foo != null && foo > 0) || (bar != null && bar > 0) || (baz != null && baz > 0)
return atLeastOnePositive;
解决方案
您可以使用Stream
并这样做:
boolean atLeastOnePositive = Stream.of(foo, bar, baz)
.anyMatch(value -> value != null && value > 0);
推荐阅读
- r - 部署 R 闪亮应用多用户并发
- c# - 脚手架 Db Context 并自动删除多余的 usings
- json - 使用 JQ 将新字段插入特定对象
- html - 在 PowerBI 中嵌入 HTML 文件内容
- google-app-engine - 部署到 gcloud 应用引擎会引发 NPM 错误
- javascript - 如何访问节点中的“primordials”对象?
- firebase - 错误:类“_JsonDocumentSnapshot”没有实例获取器“文档”
- java - Setting a char value to a 2d array
- pyspark - Databricks, dbutils, get filecount and filesize of all subfolders in Azure Data Lake gen 2 path
- rust - 在这个 Solana 智能合约中,在输入指令和账户数据之间应用 XOR 有什么作用?