java 8 可选,带有 Map<String、Set<String>>



我有一个Map<String,>getProperties返回{BUILDING=[a, b, c]}, {NEW_BUILDING=[a, b, c, d, e]}, {OLD_BUILDING=[d, e]}..

我想根据值在BUILDING, NEW_BUILDING和OLD_BUILDING之间切换。

我正在尝试

return Optional.ofNullable(properties.get("BUILDING")).map(a -> {
if(a.contains(code)) {
return callMethod;
} else {
return null;
}
}).get();

我想基于BUILDING, NEW_BUILDING和old_building返回

试试下面的代码:

Set<Map.Entry<String, Set<String>>> entries = properties.entrySet();
for (Map.Entry<String, Set<String>> entry : entries) {
Set<String> value = entry.getValue();
if (value.contains(code)) {
switch (entry.getKey()) {
case "BUILDING":
return callBuidingMethod;
case "OLD_BUILDING":
return callNewBuilding;
case "NEW_BUILDING":
return callOldBuilding;
default:
return null;
}
}
}

相关内容

最新更新