我如何在 java 中使用 groupingby
How do i make use of groupingby in java
我有一个来自 Web 服务的电影列表,我需要使用数据中的流派属性对其进行分组。我的问题与此 非常相似,但在 java 中。
来自网络服务的电影列表如下所示
[
{
"title": "AAAAAAAA",
"genres": [
"Comedy"
]
},
{
"title": "BBBBBBBBBBBB",
"genres": [
"Action", "Adventure"
]
},
{
"title": "CCCCCCCCCCCCC",
"genres": [
"Action"
]
},
{
"title": "DDDDDDDDDDDD",
"genres": [
"Comedy", "Adventure"
]
},
{
"title": "EEEEEEEEEEEEEEEEEEE",
"genres": [
"Horror"
]
}
]
这是我正在努力实现的目标,但我似乎做不对
[
{
"Action": [
{
"title": "BBBBBBBBBBBB",
"genres": [
"Action", "Adventure"
]
},
{
"title": "CCCCCCCCCCCCC",
"genres": [
"Action"
]
}
],
"Adventure": [
{
"title": "BBBBBBBBBBBB",
"genres": [
"Action", "Adventure"
]
},
{
"title": "DDDDDDDDDDDD",
"genres": [
"Comedy", "Adventure"
]
}
],
"Comedy": [
{
"title": "AAAAAAAA",
"genres": [
"Comedy"
]
},
{
"title": "DDDDDDDDDDDD",
"genres": [
"Comedy", "Adventure"
]
}
],
"Horror": [
{
"title": "EEEEEEEEEEEEEEEEEEE",
"genres": [
"Horror"
]
}
]
}
]
我试过从中的答案复制 java脚本解决方案
但我卡住了
ItemArray.stream().reduce((identity, accumulator) -> {
accumulator.getGenres().forEach((k) -> {});
return identity;
});
我认为不使用流会更容易:
Map<String, List<Item>> itemsByGenre = new HashMap<>();
for (Item item : items) {
for (String genre : item.genres()) {
itemsByGenre.computeIfAbsent(genre, g -> new ArrayList<>()).add(item);
}
}
这里的重点是,你需要按流派来展开,因为每个项目都可以在多个流派中。所以,你可以这样做:
Map<String, List<Item>> itemsByGentre = items.stream()
// Make a stream of map entries where the key is the genre, and the value is the item.
.flatMap(i -> i.genres().stream().map(g -> new AbstractMap.SimpleEntry<>(g, i))
.collect(
// Now, group by key (genre)...
Collectors.groupingBy(
Map.Entry::getKey,
// and map the entry to just the value (the item),
// and collect the items into a list.
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
我有一个来自 Web 服务的电影列表,我需要使用数据中的流派属性对其进行分组。我的问题与此
来自网络服务的电影列表如下所示
[
{
"title": "AAAAAAAA",
"genres": [
"Comedy"
]
},
{
"title": "BBBBBBBBBBBB",
"genres": [
"Action", "Adventure"
]
},
{
"title": "CCCCCCCCCCCCC",
"genres": [
"Action"
]
},
{
"title": "DDDDDDDDDDDD",
"genres": [
"Comedy", "Adventure"
]
},
{
"title": "EEEEEEEEEEEEEEEEEEE",
"genres": [
"Horror"
]
}
]
这是我正在努力实现的目标,但我似乎做不对
[
{
"Action": [
{
"title": "BBBBBBBBBBBB",
"genres": [
"Action", "Adventure"
]
},
{
"title": "CCCCCCCCCCCCC",
"genres": [
"Action"
]
}
],
"Adventure": [
{
"title": "BBBBBBBBBBBB",
"genres": [
"Action", "Adventure"
]
},
{
"title": "DDDDDDDDDDDD",
"genres": [
"Comedy", "Adventure"
]
}
],
"Comedy": [
{
"title": "AAAAAAAA",
"genres": [
"Comedy"
]
},
{
"title": "DDDDDDDDDDDD",
"genres": [
"Comedy", "Adventure"
]
}
],
"Horror": [
{
"title": "EEEEEEEEEEEEEEEEEEE",
"genres": [
"Horror"
]
}
]
}
]
我试过从中的答案复制 java脚本解决方案
ItemArray.stream().reduce((identity, accumulator) -> {
accumulator.getGenres().forEach((k) -> {});
return identity;
});
我认为不使用流会更容易:
Map<String, List<Item>> itemsByGenre = new HashMap<>();
for (Item item : items) {
for (String genre : item.genres()) {
itemsByGenre.computeIfAbsent(genre, g -> new ArrayList<>()).add(item);
}
}
这里的重点是,你需要按流派来展开,因为每个项目都可以在多个流派中。所以,你可以这样做:
Map<String, List<Item>> itemsByGentre = items.stream()
// Make a stream of map entries where the key is the genre, and the value is the item.
.flatMap(i -> i.genres().stream().map(g -> new AbstractMap.SimpleEntry<>(g, i))
.collect(
// Now, group by key (genre)...
Collectors.groupingBy(
Map.Entry::getKey,
// and map the entry to just the value (the item),
// and collect the items into a list.
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));