[英]cross map in java with another map
I have a map: 我有一张地图:
private Map<String, Map<String, ArrayList<String>>> map1;
where map contains: 地图包含:
ArrayList<String> is a list with values like : "A, B, C, .."
Map<String, ArrayList<String>> -> Here the key is: LINEA, PARCIAL, TOTAL
Map<String, Map<String, ArrayList<String>>> the key is: "B,D,F, ..." // similar
values of the map1 map1的值
{
"map1":
{
"A": { //key
"LINEA": //key internal map
["B", "C", "D", "E", "F", "G"] //List
},
"H": { //key
"LINEA": //key internal map
["H", "I", "B", "C", "J", "A"] //List
},
"I": {
"LINEA":
["I", "H", "B", "C", "J", "A"]
},
"D": {
"LINEA":
["D", "G", "E", "F", "J", "A", "K"]
},
"K": {
"LINEA":
["D", "E", "F", "G"]
},
"C": { //key
"LINEA": //key internal map
["J", "A"],
"PARCIAL": //List
["J", "A"], //Listmap
"TOTAL": //key internal map
["J", "A"] //List
},
"B": {
"PARCIAL":
["B", "H", "I", "C", "J", "A"]
},
"E": {
"PARCIAL":
["E", "G", "F", "J", "A", "K"],
"TOTAL":
["D", "G", "F", "J", "A", "K"]
},
"F": {
"TOTAL":
["F", "D"],
"PARCIAL":
["K"]
}
}
}
And I have another map: 我还有另一张地图:
Map<String,List<String>> map2;
where map cointains: 地图所在位置:
List<String> is a list with values like: 1, 2, 3, 4
Map<String,List<String>> key is : "A,C,D,H,..." // similar values of map1
list and key 清单和关键
{
"map2": {
"A": [1, 2, 3, 4],
"F": [1, 2, 4],
"D": [2,6]
"K": [7,8,9]
}
}
I need to cross the maps: 我需要越过地图:
I need reeplace the "A, B, C,E,..." values in map1 with the values in map2. 我需要将map1中的“ A,B,C,E,...”值替换为map2中的值。
the result like: 结果像:
"map1":
{
"1": { //reeplace by F
"LINEA": //key internal map
[1, 4, 2, 6], // REEMPLACE BY ["F", "D"] because is the partial list of F in map1, F = [1, 2, 4] , D = [2,6]
},
"1": { //reeplace by F
"PARCIAL": //key internal map
[7,8,9] // REEMPLACE BY ["K"]
},
.
.
.
I have a solution with 4 for. 我有一个4的解决方案。 But I don't know whether to nest 4 times is a bad practice.
但是我不知道是否嵌套4次是一个坏习惯。
Who can help me with a good solution for this problem. 谁能为我提供一个很好的解决方案。
Four nested loops is a good signal for "refactoring is needed". 四个嵌套循环是“需要重构”的一个好信号。
Ignoring the conversion of the map keys, which is not clear to me, and assuming map1
and map2
are give static fields, remapping of values into newMap
can be achieved by : 忽略映射键的转换(我不清楚),并假设
map1
和map2
为静态字段,则可以通过newMap
方式将值重新映射到newMap
中:
Map<String, Map<String, ArrayList<String>>> newMap = new HashMap<>();
for(String key : map1.keySet() ) {
newMap.put(convertKey(key), convertEntry(key));
}
}
Where convertKey
and convertEntry
are defined by 其中
convertKey
和convertEntry
由以下项定义
private static String convertKey(String key) {
// TODO add key conversion logic
return key;
}
private static Map<String, ArrayList<String>> convertEntry(String key) {
Map<String, ArrayList<String>> map = map1.get(key);
Map<String, ArrayList<String>> returnedMap = new HashMap<>();
for(String keyOfInnerMap : map.keySet()) {
ArrayList<String> newValues = mapValues(map.get(keyOfInnerMap));
returnedMap.put(keyOfInnerMap, newValues);
}
return returnedMap;
}
private static ArrayList<String> mapValues(ArrayList<String> values) {
Set<String> newValues = new HashSet<>();
for(String key : values) {
List<String> v = map2.get(key);
if(v != null) {
newValues.addAll(v);
}
}
return new ArrayList<>(newValues);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.