如何在 Scala 中从 Vector 创建 HashMap

How to create HashMap from a Vector in Scala

我有这个向量:

val imageIds = Vector(
  "XXXX1",
  "XXXX2",
  "XXXX3"
)

我目前使用以下方法创建一个数组:

def makeTheImageDataArray: Vector[JsObject] = {
  imageIds.map(SingleImageData(_, "theURL", "theStatus").asJsObject)
}

对于这种情况 class:

case class SingleImageData(ImageId: String, URL: String, Status: String) {
  def imageId: String = ImageId
  def getURL: String = URL
  def status: String = Status

  def asJsObject: JsObject = JsObject(
    "ImageId" -> JsString(imageId),
    "URL" -> JsString(getURL),
    "Status" -> JsString(status)
  )
}

产生:

Vector(
  {"ImageId":"XXXX1","URL":"theURL","Status":"theStatus"},
  {"ImageId":"XXXX2","URL":"theURL","Status":"theStatus"},
  {"ImageId":"XXXX3","URL":"theURL","Status":"theStatus"}
)

我不想生成 Vector,而是想创建一个 HashMap,以 ImageId 作为键,即:

Map(
  XXX1 -> {"URL":"theURL","Status":"theStatus"},
  XXX2 -> {"URL":"theURL","Status":"theStatus"},
  XXX3 -> {"URL":"theURL","Status":"theStatus"}
)

谁能告诉我怎么做?

asJsObject 中删除 "ImageId" -> JsString(imageId),然后

imageIds.map(id => id -> SingleImageData(id, "theURL", "theStatus").asJsObject).toMap

或者如果 SingleImageData 不需要知道 id,则从 SingleImageData 中完全删除 ImageID,并且

imageIds.map(_ -> SingleImageData("theURL", "theStatus").asJsObject).toMap