将列表转换为元素映射 -> scala 中的列表(元素)
我有一个文档列表,其中文档的所有者是用户。
将此列表转换为用户到其拥有的文档列表的地图的最优雅的方法是什么?
因此,例如我有:
"doc1" owned by user "John"
"doc2" owned by user "Frank"
"doc3" owned by user "John"
我最终应该得到一张地图:
"John" -> List("doc1", "doc3"), "Frank" -> List("doc2")
我可以想到一种方法,从文档中获取所有唯一用户,并为每个用户过滤文档列表,使其成为他们拥有的文档列表,但是我想知道是否有一种方法可以使用固定数量的列表遍历来防止列表很大时出现任何性能问题。
I have a list of documents, where a Document has an owner which is a User.
What is the most elegant way of transforming this list into a Map of Users to the List of Documents that they own?
So for example I have:
"doc1" owned by user "John"
"doc2" owned by user "Frank"
"doc3" owned by user "John"
I should end up with a map of:
"John" -> List("doc1", "doc3"), "Frank" -> List("doc2")
I can think of one way which would be to grab all unique users from the documents and for each of them filter the document list to just be the ones they own, but I'm wondering if there's a way that uses a fixed number of passes through the list to prevent any performance problems if the list is big.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用分组依据:
Use groupBy: