我有一张带有
"task(s)": ["epochdate1", "epochdate2"]
的地图,如何反转(反转)该地图?
例如
task_1 => [date1, date2, date3, date5]
task_2 => [date4, date5]
task_3 => [date2, date3, date5]
task_4 => [date4, date5]
反转后会是
date1 => [task_1]
date2 => [task_1, task_3]
date3 => [task_1, task_3]
date4 => [task_2, task_4]
date5 => [task_1, task_2, task_3, task_4]
代码
public static void main(String[] args) {
Map<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> t1List = new ArrayList<String>();
t1List.add("date1");
t1List.add("date2");
t1List.add("date3");
t1List.add("date5");
ArrayList<String> t2List = new ArrayList<String>();
t2List.add("date4");
t2List.add("date5");
ArrayList<String> t3List = new ArrayList<String>();
t3List.add("date2");
t3List.add("date3");
t3List.add("date5");
ArrayList<String> t4List = new ArrayList<String>();
t4List.add("date4");
t4List.add("date5");
myMap.put("task_1", t1List);
myMap.put("task_2", t2List);
myMap.put("task_3", t3List);
myMap.put("task_4", t4List);
Map<String, ArrayList<String>> reversedMap = Test.getReversedMap(myMap);
}
只需实施即可:
Map<String, ArrayList<String>> getReversedMap(Map<String, ArrayList<String>> myMap){
Map<String, ArrayList<String>> result = new HashMap<>();
for(String key : myMap.keySet()){
for(String val : myMap.get(key)){
if(!result.containsKey(val)){
result.put(val, new ArrayList());
}
result.get(val).add(key);
}
}
return result;
}
没有捷径。您必须迭代原始映射的entrySet,对于找到的每个日期值,将一个新条目放入新映射中,并将匹配的任务作为第一个值,或者将该任务添加到现有条目的列表中(如果日期已在新地图中。
我会用番石榴来做这个。尝试这样的事情:
Map<K, V> map = ...;
ListMultimap<V, K> inverse = Multimaps.invertFrom(Multimaps.forMap(map),
ArrayListMultimap.<V,K>create());
这是 Java 8+ 中使用流的解决方案:
Map<String, List<String>> invertedMap = myMap.entrySet().stream()
.flatMap(e -> e.getValue().stream()
.map(v -> Map.entry(e.getKey(), v)))
.collect(Collectors.groupingBy(
Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
// Stream<Map.Entry<String, ArrayList<String>>>
Map<String, List<String>> invertedMap = myMap.entrySet().stream()
// Stream<Map.Entry<String, String>>
.flatMap(e -> e.getValue().stream().map(v -> Map.entry(e.getKey(), v)))
// Map<String, List<String>>
.collect(Collectors.groupingBy(
Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey, Collectors.toList())));