假设我有一个country对象列表,其中包含在该国家使用的语言列表,如下所示:
class Country {
List languages;
}我想创建以下格式的地图:Map>,使得每种语言都映射到国家对象列表。例如:
"French" -> [Country:France, Country:Canada],
"English" -> [Country:UK, Country:US]性能是这里的一个问题,所以我想避免多次迭代和查找。我试过使用groupingBy,但是我该如何flatMap密钥集?
例如,这将导致Map, List>
countries.stream()
.collect(Collectors.groupingBy(country -> country.getLanguages(), toList()));发布于 2019-03-26 23:21:09
既然您似乎很关心性能,那么就不要使用streams来完成这个简单的任务:
Map> countriesByLanguage = new HashMap<>();
for (Country country : countries) {
for (String language : country.getLanguages()) {
countriesByLanguage.computeIfAbsent(language, k -> new ArrayList<>())
.add(country);
}
}发布于 2021-02-25 04:23:07
您可以使用以下命令来完成此操作小溪中的小溪如下所示:首先迭代国家列表,然后迭代嵌套的语言列表并准备«language, country»对,然后收集它们来映射:
public static void main(String[] args) {
List countries = List.of(
new Country("France", List.of("French")),
new Country("Canada", List.of("French")),
new Country("UK", List.of("English")),
new Country("US", List.of("English")));
Map> map = countries.stream()
// Stream>
.flatMap(country -> country.getLanguages().stream()
.map(lang -> Map.entry(lang, country)))
.collect(Collectors.toMap(
// key - language
Map.Entry::getKey,
// value - List
entry -> new ArrayList<>(List.of(entry.getValue())),
// merge duplicates, if any
(list1, list2) -> {
list1.addAll(list2);
return list1;
}
));
// output
map.forEach((k, v) -> System.out.println(k + "=" + v));
//English=[Country:UK, Country:US]
//French=[Country:France, Country:Canada]
}static class Country {
String name;
List languages;
public Country(String name, List languages) {
this.name = name;
this.languages = languages;
}
public List getLanguages() {
return languages;
}
@Override
public String toString() {
return "Country:" + name;
}
}发布于 2019-03-26 23:14:14
这样就可以了:
countries.stream()
.flatMap(country -> country.getLanguages()
.stream()
.map(lang -> new SimpleEntry<>(lang,
new ArrayList<>(Arrays.asList(country)))))
.collect(Collectors.toMap(
Entry::getKey,
Entry::getValue,
(l1, l2) -> {
l1.addAll(l2);
return l2;
}));https://stackoverflow.com/questions/55360374
复制相似问题