Group 2 case classes

42 views
Skip to first unread message

Michael Larsson

unread,
May 17, 2013, 4:43:27 AM5/17/13
to scala...@googlegroups.com
Hi group.

I have a question about grouping 2 lists of case classes together, and in this example sorting employees into EmployeeDepartment.

1. case class EmployeeDepartment( @Key("_id") id:ObjectId, name:String)
2. case class Employees( @Key("_id") id:ObjectId, name:String, employeeDepartmentName:String)

Basically, gettin a new list or map (don't know what would be better) looking like this:

(EmployeeDepartment(12456, "Marketing department"), List( 123456, "Michael Larsson" ), List( 123456, "etc" ))

Thanks

Johannes Rudolph

unread,
May 17, 2013, 5:00:38 AM5/17/13
to Michael Larsson, scala-user
You can use `groupBy` to group a sequence of values by some
classification [1]. From your description it is not clear how your
model is supposed to work in detail so it's hard to give a more
precise answer. Some questions:

1. Why is the class called "Employees" (plural)?
2. Is the department matched by name? Why? Where do the target values come from?
3. What does your supposed result mean? Is it a valid Scala expression?

Actually, I'd guess if you start with stating your question in a way
that is fully specified you will already have 90% of the answer found
out yourself :)

HTH

Johannes

[1] http://www.scala-lang.org/api/current/index.html#scala.collection.Seq
> --
> You received this message because you are subscribed to the Google Groups
> "scala-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scala-user+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Michael Larsson

unread,
May 17, 2013, 5:18:26 AM5/17/13
to scala...@googlegroups.com, Michael Larsson, johannes...@googlemail.com
Hi Johannes and thanks for your answear.

1. Misstake by me, I see that now. It should be Employee.
2. The Employee class is matched by it's id. I see it says :String but it should be ObjectId. The classes contains more parameters (ie, phone, email etc.) and missed that
3. When looking in to it, basically a Map("Department 1" -> List("Employee 1", "Employee 2"), "Department 2" -> List("Employee 1", "Employee 2"))

I'm I making any sense? :)

Alec Zorab

unread,
May 17, 2013, 5:31:38 AM5/17/13
to Michael Larsson, scala-user, Johannes Rudolph
case class Employee(Department: DepartmentId, Name:String) //etc, etc
val employees : List[Employee] = ???
val departments : Map[DepartmentId, List[Employee]] = employees.groupBy(_.Department)

Is that what you want?

Michael Larsson

unread,
May 17, 2013, 5:37:41 AM5/17/13
to scala...@googlegroups.com, Michael Larsson, Johannes Rudolph
Don't know if this is a "good" way of doing it but it gives me the result I'm looking for:

for(department <- departments) {
      val emp= for(employee <- employees; if(department .id== employee .employeeDepartment)) yield employee 
      println(Map(department  -> emp))
}

Alec Zorab

unread,
May 17, 2013, 6:07:12 AM5/17/13
to Michael Larsson, scala-user, Johannes Rudolph
uh. Are you sure?

presuming you meant to return something, not just print a map with only a single entry, that'd give you a Seq[Map[Department, List[Employee]] which probably isn't what you actually meant.

I supect you wanted this:

  case class DepartmentId(Id:String)
  case class Department(Id: DepartmentId)
  case class Employee(Department: DepartmentId, Name:String) //etc, etc

  val departments: List[Department] = ???
  val employees : List[Employee] = ???
  val employeesByDepartmentId: Map[DepartmentId, List[Employee]] = employees.groupBy(_.Department)
  val result : Map[Department, List[Employee]] = departments.map(d => d -> employeesByDepartmentId(d.Id)).toMap


--

Michael Larsson

unread,
May 17, 2013, 7:33:00 AM5/17/13
to scala...@googlegroups.com, Michael Larsson, Johannes Rudolph
Yes that's what I get of this: (ugly I know)

val departmentAndEmployees:List[Map[EmployeeDepartment, List[Employees]]] = {
      for(department <- departments) yield { Map(department -> (for(employee <- employees; if(department.id== employee.employeeDepartment)) yield employee)) }
    }

However, I'll try the example you gave me. :)
Thanks
Reply all
Reply to author
Forward
0 new messages