使用复合键将树状地图转换为普通地图
Converting tree-like map into plain map with composite keys
我试图在 Java 中做一些事情,但我被困在这个问题上,我真的不知道如何转换它。我尝试了 for 循环,但我需要做很多 for 循环,我想保持我的代码干净。
我有一个看起来像这样的地图。
Map 1:
A: Object
B: Object
C: Map 2
D: Object
E: Object
F: Map 3
G: Object
(Here could be another map that could go really far, etc)
H: Map 4
I: Object
J: Object
(Here could also be more)\
K: Object
(Here could also be more)
请注意,这只是一个示例,映射、键等的数量可能不同,键名也可能不同。键名不能包含点。
C、F、H为地图,其中地图C包含D、E、F、H、K,地图F包含G等
如何将其转换为如下所示的一张地图:
A: Object
B: Object
C.D: Object
C.E: Object
C.K: Object
C.F.G: Object
C.H.I: Object
C.H.J: Object
键的顺序无关紧要。
像这样:
public Map<String, Object> convert (Map<String, Object> input, String key) {
Map<String, Object> res = new HashMap<>();
for (Entry<String, Object> e : input.entrySet()) {
String newKey = key == "" ? e.getKey() : (key + "." + e.getKey());
if (e.getValue() instanceof Map) {
res.putAll(convert((Map) e.getValue(), newKey)); // recursive call
} else {
res.put(newKey, e.getValue());
}
}
return res;
}
现在你在你的输入地图上调用它:
Map<String, Object> converted = convert(input, "");
沿着这些方向尝试一些东西
假设您的地图是
地图
public class ObjectMapper
{
Map<Integer,Object> subMap;
}
承认您的对象可以是字符串或映射
假设您将 "path" 调用到这样的对象
“1#2#4#5”
Map<Integer, Object> someMap;
//assign map or load it from somewhere
String searchPath = "1#2#4#5";
String[] splitPath = searchPath .split("#");
String searchResult = null;
boolean resultFound = false;
boolean resultWrongPlace = false;
Map<Integer, Object> mapToSearch = null;
for(int i = 0; i < splitPath.length; i++)
{
if (resultFound == true)
{
resultWrongPlace = true;
break;
}
if (i == 0)
{
mapToSearch = someMap;
}
Object currentObj = mapToSearch.get(splitPath[Integer.parseInt(splitPath[i])])
if (currentObj instanceof String)
{
searchResult = (String) currentObj ;
resultFound = true;
}
else if (currentObj instanceof ObjectMapper)
{
mapToSearch = (ObjectMapper) currentObj ;
resultFound = false;
}
}
if (resultFound == true && resultWrongPlace == true)
{
throw new Exception("result was found but the path that lead to a result was different from the indended path!");
}
else if (resultFound == false)
{
throw new Exception("no result was found searching your expression path!");
}
我想说的是按照这些思路做的事情
我试图在 Java 中做一些事情,但我被困在这个问题上,我真的不知道如何转换它。我尝试了 for 循环,但我需要做很多 for 循环,我想保持我的代码干净。
我有一个看起来像这样的地图。
Map 1:
A: Object
B: Object
C: Map 2
D: Object
E: Object
F: Map 3
G: Object
(Here could be another map that could go really far, etc)
H: Map 4
I: Object
J: Object
(Here could also be more)\
K: Object
(Here could also be more)
请注意,这只是一个示例,映射、键等的数量可能不同,键名也可能不同。键名不能包含点。
C、F、H为地图,其中地图C包含D、E、F、H、K,地图F包含G等
如何将其转换为如下所示的一张地图:
A: Object
B: Object
C.D: Object
C.E: Object
C.K: Object
C.F.G: Object
C.H.I: Object
C.H.J: Object
键的顺序无关紧要。
像这样:
public Map<String, Object> convert (Map<String, Object> input, String key) {
Map<String, Object> res = new HashMap<>();
for (Entry<String, Object> e : input.entrySet()) {
String newKey = key == "" ? e.getKey() : (key + "." + e.getKey());
if (e.getValue() instanceof Map) {
res.putAll(convert((Map) e.getValue(), newKey)); // recursive call
} else {
res.put(newKey, e.getValue());
}
}
return res;
}
现在你在你的输入地图上调用它:
Map<String, Object> converted = convert(input, "");
沿着这些方向尝试一些东西 假设您的地图是 地图
public class ObjectMapper
{
Map<Integer,Object> subMap;
}
承认您的对象可以是字符串或映射 假设您将 "path" 调用到这样的对象 “1#2#4#5”
Map<Integer, Object> someMap;
//assign map or load it from somewhere
String searchPath = "1#2#4#5";
String[] splitPath = searchPath .split("#");
String searchResult = null;
boolean resultFound = false;
boolean resultWrongPlace = false;
Map<Integer, Object> mapToSearch = null;
for(int i = 0; i < splitPath.length; i++)
{
if (resultFound == true)
{
resultWrongPlace = true;
break;
}
if (i == 0)
{
mapToSearch = someMap;
}
Object currentObj = mapToSearch.get(splitPath[Integer.parseInt(splitPath[i])])
if (currentObj instanceof String)
{
searchResult = (String) currentObj ;
resultFound = true;
}
else if (currentObj instanceof ObjectMapper)
{
mapToSearch = (ObjectMapper) currentObj ;
resultFound = false;
}
}
if (resultFound == true && resultWrongPlace == true)
{
throw new Exception("result was found but the path that lead to a result was different from the indended path!");
}
else if (resultFound == false)
{
throw new Exception("no result was found searching your expression path!");
}
我想说的是按照这些思路做的事情