Java 8 groupingBy: 单列简单分组

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

Java 8 groupingBy: Simple Grouping by a Single Column

问题

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

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

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

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

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

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

but I have this compilation error:

Required type:
Collector
&lt;? super List&lt;IUserPost&gt;,
A,
R&gt;
Provided:
Collector
&lt;IUserPost,
capture of ?,
Map&lt;IUser, List&lt;IUserPost&gt;&gt;&gt;
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()收集到一个映射中,将键(用户)映射为自身,将值映射为其帖子的列表:

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

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

postsPerUser =
    autorisationUsersRepository.findById(dateReference).stream()
    .map(UsersMapper::map) // Stream&lt;IUser&gt;
    .flatMap(IUser::getPosts) // Stream&lt;IUserPost&gt;    
    .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:

postsPerUser =
    autorisationUsersRepository.findById(dateReference).stream()
    .map(UsersMapper::map) // Stream&lt;IUser&gt;
    .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.

postsPerUser =
    autorisationUsersRepository.findById(dateReference).stream()
    .map(UsersMapper::map) // Stream&lt;IUser&gt;
    .flatMap(IUser::getPosts) // Stream&lt;IUserPost&gt;    
    .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:

确定