java - 字符串连接空值并检索空值java
问题描述
我正在使用 Stream.of 将几个字符串值与分隔符“:”连接起来,其中一个可以为空,但是当我将它检索到字符串数组时,我希望其中有固定数量的元素大批。我想知道字符串数组中的哪一个为空。例如,我通过使用连接了一个字符串
Stream.of("abc", "def", "ghi", null)
.collect(Collectors.joining(":"));
它将是“abc:def:ghi:null”。然后将其拆分为字符串数组,我使用
final String[] strings = "abc:def:ghi:null".split(":");
但是字符串 [3] 是“null”而不是 null。有没有办法将“null”转换为空值?
解决方案
您应该将null
映射作为后处理处理:
String[] strings = "abc:def:ghi:null".split(":");
strings = Arrays.stream(strings).map(s-> s.equals("null")? null : s).toArray(String[]::new);
System.out.println(strings[3] == null);
打印true
。
请注意,在这里您不知道是否在流中的原点使用null
了"null"
String ,因为一旦加入 String ,您就无法区分它们。
推荐阅读
- jenkins - 如何从 Jenkins 声明性管道中的文本参数中检索所有行?
- java - JDK 从 1.7 更新到 1.8 后,dotCMS 2.5.6 无法启动
- django-models - 如何通过 django 的管理面板上传指向 href 函数的链接?
- api - 通过 Nitro API (Citrix Netscaler) 使用主题备用名称的 CSR 请求
- .net - 将特定控制器映射到特定 Kestrel 端点
- flutter - 如何使用 Flutter 的 onWillpop 进行 BottomNavigationBar 页面过渡?
- java - Spring Boot - 并行执行 setter 语句以优化响应时间
- docker - Jenkins Pipeline NPM 集成创建空的 package.json
- android - 在协程上下文中返回 Kotlin 结果会引发异常
- java - AppleId 授权码的 Java 验证