Java 8 groupingBy: 单列简单分组

huangapple go评论101阅读模式
英文:

Java 8 groupingBy: Simple Grouping by a Single Column

问题

我想使用Java 8中的groupingBy收集器按单个列进行简单分组,所以我做了以下操作:

  1. Map<IUser, List<IUserPost>> postsPerUser =
  2. autorisationUsersRepository.findById(dateReference)
  3. .stream()
  4. .map(UsersMapper::map)
  5. .flatMap(user -> user.getPosts().stream())
  6. .collect(Collectors.groupingBy(IUserPost::getUser));

但是我遇到了这个编译错误:

  1. 所需类型
  2. Collector<? super List<IUserPost>, A, R>
  3. 已提供类型
  4. Collector<IUserPost, capture of ?, Map<IUser, List<IUserPost>>>
  5. 原因不存在类型变量的实例使得List<IUserPost>符合IUserPost
英文:

I want to do simple Grouping by a Single Column using Java 8 groupingBy Collector, so I did this:

  1. Map&lt;IUser, List&lt;IUserPost&gt;&gt; postsPerUser =
  2. autorisationUsersRepository.findById(dateReference)
  3. .stream()
  4. .map(UsersMapper::map) -&gt; Stream&lt;IUser&gt;
  5. .map(IUser::getPosts) -&gt; Stream&lt;List&lt;IUserPost&gt;&gt;
  6. .collect(groupingBy(IUserPost::getUser));

but I have this compilation error:

  1. Required type:
  2. Collector
  3. &lt;? super List&lt;IUserPost&gt;,
  4. A,
  5. R&gt;
  6. Provided:
  7. Collector
  8. &lt;IUserPost,
  9. capture of ?,
  10. Map&lt;IUser, List&lt;IUserPost&gt;&gt;&gt;
  11. reason: no instance(s) of type variable(s) exist so that List&lt;IUserPost&gt; conforms to IUserPost

答案1

得分: 1

好的,以下是您要求的翻译内容:

好吧,List&lt;IUserPost&gt; 不是 IUserPost,所以你可以以那种方式对它们进行分组。

你可以直接使用Collectors.toMap()收集到一个映射中,将键(用户)映射为自身,将值映射为其帖子的列表:

  1. postsPerUser =
  2. autorisationUsersRepository.findById(dateReference).stream()
  3. .map(UsersMapper::map) // Stream&lt;IUser&gt;
  4. .collect(toMap(user -&gt; user, IUser::getPosts)) // Map&lt;IUser, List&lt;IUserPost&gt;&gt;

或者你可以使用flatMap获得一个Stream&lt;IUserPost&gt;,然后可以按用户进行分组。

  1. postsPerUser =
  2. autorisationUsersRepository.findById(dateReference).stream()
  3. .map(UsersMapper::map) // Stream&lt;IUser&gt;
  4. .flatMap(IUser::getPosts) // Stream&lt;IUserPost&gt;
  5. .collect(groupingBy(IUserPost::getUser)) // Map&lt;IUser, List&lt;IUserPost&gt;&gt;
英文:

Well, a List&lt;IUserPost&gt; is not an IUserPost, so you can group those in that way.

You can collect directly to a map using Collectors.toMap(), mapping the key (user) to itself and the value to the list of their posts:

  1. postsPerUser =
  2. autorisationUsersRepository.findById(dateReference).stream()
  3. .map(UsersMapper::map) // Stream&lt;IUser&gt;
  4. .collect(toMap(user -&gt; user, IUser::getPosts)) // Map&lt;IUser, List&lt;IUserPost&gt;&gt;

Or you can use flatMap to get a Stream&lt;IUserPost&gt;, which you can then group by user.

  1. postsPerUser =
  2. autorisationUsersRepository.findById(dateReference).stream()
  3. .map(UsersMapper::map) // Stream&lt;IUser&gt;
  4. .flatMap(IUser::getPosts) // Stream&lt;IUserPost&gt;
  5. .collect(groupingBy(IUserPost::getUser)) // Map&lt;IUser, List&lt;IUserPost&gt;&gt;

huangapple
  • 本文由 发表于 2020年10月1日 16:52:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64151969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定