首页 > 解决方案 > 如果 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 表达式的新手。

标签: javalambda

解决方案


您可能是指三元运算符而不是 lambda 表达式。根据您的要求,这是有道理的。

fullAlert.getSupportingData().forEach(data -> dataMap.put(data.getKey(), data.getValue() != null && !data.getValue().isEmpty()? data.getValue().replace("\\","\\\\") : data.getValue()));

推荐阅读