【发布时间】:2022-09-30 21:08:12
【问题描述】:
我很难转换一个 Map ,它有一些整数作为键,随机字符串列表作为值。
e.g.
1 = [\"a\", \"b\", \"c\"]
2 = [\"a\", \"b\", \"z\"]
3 = [\"z\"]
到具有以该 int 作为键的整数的不同字符串的 Map
e.g.
a = [1, 2]
b = [1, 2]
c = [1]
z = [2,3]
这是我到目前为止得到的:
Map<Integer, List<String>> integerListMap; <- Initial list already populated
List<String> distinctStrings = new ArrayList<>();
SortedMap<String, List<Integer>> stringListSortedMap = new TreeMap<>();
for(Integer i: integers) {
integerListMap.put(i, strings);
distinctStrings.addAll(strings);
}
distinctStrings = distinctStrings.stream().distinct().collect(Collectors.toList());
for(String s : distinctStrings) {
distinctStrings.put(s, )
}
提前致谢
-
遍历您的源代码
Map并直接在该循环中填充结果。无需创建distinctStrings列表。
标签: java dictionary