无法在Scala中创建嵌套对象

时间:2015-08-20 07:46:37

标签: java scala

我正在使用具有父子参考的对象列表

case class EmployeeLevel(id: Long, name: String, parentId: Long,
                         item: List[EmployeeLevel])

val employeeList = List(
  EmployeeLevel(1L, "Manoj", 0L, Nil),
  EmployeeLevel(2L, "Sv", 1L, Nil),
  EmployeeLevel(3L, "Cp", 2L, Nil),
  EmployeeLevel(4L, "Manju", 3L, Nil))

如何创建树状结构或将其排列在分层次序中

val employeeList = List(
  EmployeeLevel(1L, "Manoj", 0L, List(
    EmployeeLevel(2L, "Sv", 1L, List(
      EmployeeLevel(3L, "Cp", 2L, List(
        EmployeeLevel(4L, "Manju", 3L, Nil))))))))

或者这是我应该得到的最终json

[
    {
        "id": 1,
        "name": "Manoj",
        "parentId": 0,
        "item": [
            {
                "id": 2,
                "name": "Sv",
                "parentId": 1,
                "item": [
                    {
                        "id": 3,
                        "name": "Cp",
                        "parentId": 2,
                        "item": [
                            {
                                "id": 4,
                                "name": "Manju",
                                "parentId": 3,
                                "item": [  ]                         

                            }
                        ]
                    }
                ]
            }
        ]
    }
]

我试过这个:

 def arrange(employee1:List[EmployeeLevel],accumulator:List[EmployeeLevel]): List[EmployeeLevel] ={
    @tailrec   def arrange1(employeeLevel:List[EmployeeLevel],accu:List[EmployeeLevel]):List[EmployeeLevel] ={
      if(employeeLevel.nonEmpty){
        val headd=employeeLevel.head
        val taill=employeeLevel.tail
        val tt=taill.filter(_.parentId==headd.id)
        val qq= List(headd.copy(item = tt))
        arrange1(taill,accu++qq)
      }else{
        accu
      }

    }
    arrange1(employee1,Nil)
  }

但它仅适用于一个级别

1 个答案:

答案 0 :(得分:5)

这应该可以解决问题

object App {
  case class EmployeeLevel(id: Long, name: String, parentId: Long,
    item: List[EmployeeLevel])

  val employeeList = List(
    EmployeeLevel(1L, "Manoj", 0L, Nil),
    EmployeeLevel(2L, "Sv", 1L, Nil),
    EmployeeLevel(3L, "Cp", 2L, Nil),
    EmployeeLevel(4L, "Manju", 3L, Nil),
    EmployeeLevel(5L, "foobar", 2L, Nil),
    EmployeeLevel(6L, "barfoo", 42L, Nil))

  def main(args : Array[String]) {
    val nestedEmployees = createNestedEmployeesList(employeeList)

    println(nestedEmployees)
  }

  def createNestedEmployeesList(employees: List[EmployeeLevel]): Iterable[EmployeeLevel] = {
    val children = employees.groupBy(_.parentId)

    val rootNodes = findRootNodes(employees)

    rootNodes.flatMap(createChildrenList(_, children))
  }

  def createChildrenList(id: Long, children: Map[Long, List[EmployeeLevel]]): List[EmployeeLevel] = {
    children.get(id) match {
      case Some(employees) => 
        employees.map(employee => employee.copy(item = createChildrenList(employee.id, children)))
      case None => Nil
    }
  }

  def findRootNodes(employees: List[EmployeeLevel]): Set[Long] = {
    val parentIds = employees.map(_.parentId).toSet
    val nodeIds = employees.map(_.id).toSet

    parentIds &~ nodeIds
  }
}

该算法为您提供以下输出

List(EmployeeLevel(1,Manoj,0,List(EmployeeLevel(2,Sv,1,List(EmployeeLevel(3,Cp,2,List(EmployeeLevel(4,Manju,3,List()))), EmployeeLevel(5,foobar,2,List()))))))