Q:

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.
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.
I basically would like all the matches that have the same Bar to be disassembled and their Fus put into a list. Kind of like a Map[Bar, List[Fu]], except that I have a type for that.
The Result class might as well use Bars instead of the name String but I really do not need the other Bar information. 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. 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? I can easily turn each Match into a Result, but then I will end up with many Result objects with the same name...
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.

A:

You were on the right track with 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)))

