【发布时间】:2021-09-23 13:23:33
【问题描述】:
从 java 转换为 kotlin
java代码
public void logEvent(String eventName, @Nullable Map<String, String> customParams) {
if (customParams == null) {
customParams = new HashMap<>();
}
customParams.put(OTHER_REQUIRED_KEY, OTHER_REQUIRED_VALUE);
service.doLogEvent(eventName, customParams);
}
kotlin 代码
fun logEvent(eventName: String, customParams: Map<String, String>?) {
var customParamsMap = HashMap<String, String>()
if (customParams != null) {
customParamsMap.putAll(customParams)
}
customParamsMap[OTHER_REQUIRED_KEY] = OTHER_REQUIRED_VALUE
service.doLogEvent(eventName, customParamsMap)
}
无论传入的映射是否为空,kotlin 代码都会创建临时映射。
有没有更好的方法来避免这种地图创建?
【问题讨论】:
标签: kotlin kotlin-java-interop mutablemap