我从我的静态配置中读取了以下格式的地图:
Map<String, Map<String, List<String>>> dependentPluginEntityMapString
. 此映射中的字符串值实际上来自 ENUM,映射的正确且必需的表示形式是Map<ENUM_A, Map<ENUM_A, List<ENUM_B>>>
.
ENUM_A {
APPLE, BANANA
}
ENUM_B {
ONION, RADDISH
}
- 如何将字符串映射转换为带有枚举的映射以
Map<ENUM_A, Map<ENUM_A, List<ENUM_B>>>
提高类型安全性?
我知道,我可以迭代字符串映射(使用 for 或流)并根据需要创建一个带有枚举的新映射,但寻找更好/更高效和优雅的方式来做到这一点?
这是我的蛮力解决方案。我能做得更好吗?
final Map<ENUM_A, Map<ENUM_A, List<ENUM_B>>> dependentPluginEntityMap = new HashMap<>();
for (Map.Entry<String, Map<String, List<String>>> dependentPluginEntry:
dependentPluginEntityMapFromAppConfig.entrySet()) {
final Map<ENUM_A, List<ENUM_B>> independentPluginMapForEntry = new HashMap<>();
if (MapUtils.isNotEmpty(dependentPluginEntry.getValue())) {
for (Map.Entry<String, List<String>> independentPluginEntry:
dependentPluginEntry.getValue().entrySet()) {
if (CollectionUtils.isNotEmpty(independentPluginEntry.getValue())) {
independentPluginMapForEntry.put(ENUM_A.valueOf(independentPluginEntry.getKey()),
independentPluginEntry.getValue().stream().map(value -> ENUM_B.valueOf(value))
.collect(Collectors.toList()));
}
}
}
dependentPluginEntityMap.put(ENUM_A.valueOf(dependentPluginEntry.getKey()),
independentPluginMapForEntry);
}
- 我应该将字符串映射转换为 ENUMMAP ,而不是使用枚举键映射吗?它适用于我的嵌套地图结构吗?
任何线索表示赞赏。