【问题标题】:How do i make use of groupingby in java我如何在java中使用groupingby
【发布时间】:2021-09-20 15:15:48
【问题描述】:

我有一个来自网络服务的电影列表,我需要使用数据中的流派属性对其进行分组。我的问题与groupby ngFor angular2 非常相似,但在 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"
                ]
            }
        ]
    }
]

我尝试从答案中复制 javascript 解决方案 groupby ngFor angular2 但我卡住了

    ItemArray.stream().reduce((identity, accumulator) -> {    
        accumulator.getGenres().forEach((k) -> {});
        return identity;
    });

【问题讨论】:

  • 先尝试使用标准的 for 循环。每当我为如何构建流而苦苦挣扎时,使用命令式版本会有很大帮助

标签: java arrays spring-boot java-stream


【解决方案1】:

我认为不使用流会更容易:

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())));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    相关资源
    最近更新 更多