简体   繁体   English

将地图迭代器映射到地图

[英]Map Iterator into map

I have the following situation: I receive an Iterator of a certain type and would like to turn it into a particular map. 我有以下情况:我收到某种类型的Iterator,并希望将其变成特定的映射。 I feel like there is probably a very easy way to do this but I just cannot seem to figure it out :) Here is some code to make things easier to understand: (I shortened the case classes to a bare minimum) 我觉得可能有一种非常简单的方法来执行此操作,但是我似乎无法弄清楚:)下面的代码使事情更容易理解:(我将案例类缩短到最低限度)

case class Fu(title: String)
case class Bar(name: String)
case class Match(f: Fu, b: Bar)
case class Result(name: String, fus: List[Fu])

val input: Iterator[Match] = ...

What I would like to have in the end is something like this: 我最后想要的是这样的:

val output: Iterator[Result]

Here is the tricky part: The matches are unique, but naturally, each Fu could be matched so multiple Bars. 这是棘手的部分:匹配项是唯一的,但是自然地,每个Fu都可以匹配,因此有多个Bar。 I basically would like all the matches that have the same Bar to be disassembled and their Fus put into a list. 我基本上希望分解具有相同Bar的所有比赛并将其Fus放入列表中。 Kind of like a Map[Bar, List[Fu]], except that I have a type for that. 有点像Map [Bar,List [Fu]],除了我有一个类型。 The Result class might as well use Bars instead of the name String but I really do not need the other Bar information. Result类也可以使用Bars而不是名称String,但是我真的不需要其他Bar信息。 But that would not matter too much. 但这并不太重要。 And the output type is not all that important either, be it Iterator, Iterable, List, or anything else. 输出类型也不是那么重要,它是Iterator,Iterable,List或其他任何东西。 As long as the mapping works properly. 只要映射正常工作。

At first, I thought groupBy on Iterable would do exactly what I want but I can either not figure out how to use groupBy properly or it is the wrong function for the job :) Using map seems to be a good start, but then how do I build the list? 起初,我以为Iterable上的groupBy可以完全实现我想要的功能,但是我无法弄清楚如何正确使用groupBy,或者这是该工作的错误功能:)使用map似乎是一个不错的开始,但是接下来该怎么做我建立清单? I can easily turn each Match into a Result, but then I will end up with many Result objects with the same name... 我可以轻松地将每个Match转换成一个Result,但是最后我会得到许多同名的Result对象。

Thank you very much, any help is appreciated! 非常感谢,感谢您的帮助!

Edit: Maybe I should make clear that it is not about matching the Strings. 编辑:也许我应该说清楚,这与匹配字符串无关。 They are just example attributes for the two case classes. 它们只是两个案例类的示例属性。 All Fus and Bars have already been matched properly into the Match objects. 所有Fus和Bars已经正确匹配到Match对象中。

You were on the right track with groupBy . 您与groupBy一起groupBy在正确的轨道上。 Here's how it would look: 外观如下:

val input: Iterator[Match] = Iterator(
  Match(Fu("A"), Bar("1")),
  Match(Fu("B"), Bar("2")),
  Match(Fu("C"), Bar("1")),
  Match(Fu("D"), Bar("2")))

val output =
  input.toList
    .groupBy { case Match(f, Bar(name)) => name }
    .map { case (name, fus) => Result(name, fus.map(_.f)) }

 output foreach println                         //> Result(1,List(Fu(A), Fu(C)))
                                                //| Result(2,List(Fu(B), Fu(D)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM