java - 如果 lambda 表达式中的条件更改 java 中的值
问题描述
这是我要修改的代码片段。
fullAlert.getSupportingData().forEach( data ->
dataMap.put(data.getKey(), data.getValue())
);
我想检查是否data.getValue()
返回empty 或 null。如果是的话
dataMap.put(data.getKey(), data.getValue())
否则我想替换反斜杠
dataMap.put(data.getKey(), data.getValue().replace("\\","\\\\"))
我是 lambda 表达式的新手。
解决方案
您可能是指三元运算符而不是 lambda 表达式。根据您的要求,这是有道理的。
fullAlert.getSupportingData().forEach(data -> dataMap.put(data.getKey(), data.getValue() != null && !data.getValue().isEmpty()? data.getValue().replace("\\","\\\\") : data.getValue()));
推荐阅读
- r - 从 exec 目录执行脚本
- javascript - 将参数从 Vue 发送到 Laravel 控制器以更新道具
- odoo-13 - ir.actions.act_window 中的 src_model 在哪里?
- linux - 如何在 Ubuntu 上运行 Kotlin?
- java - 如何将自定义 jar 文件添加到 pom.xml
- mysql - 有没有办法使用 ER 图作为数据库模式?如果是,我该如何使用它?
- .net - dotnet restore:排除项目
- javascript - 如何在fullcalendar jquery中更改参数名称“title”、“start”和“end”
- sql - 在 PostgreSQL 中,有效地为另一个表中的每一行使用一个表
- c# - 使用 Autofac 4.2.1 注入已注册的命名服务未按预期工作