Scala 将listbuffer中的元素添加到映射中(更新映射)

Scala 将listbuffer中的元素添加到映射中(更新映射),scala,Scala,我是scala新手,在一个具有以下结构的映射中有一个listbuffer: class Person(var name: String, var age: Int,note: ListBuffer[Note]) class Note( email: String, note: Int) var m = Map[Tuple3[Int,Int,Int],Person]() 如何更新映射以将新元素添加到listbuffer中。使用所需的注释分别创建listbuffer。然后,只需按如下方

我是scala新手,在一个具有以下结构的映射中有一个listbuffer:

class Person(var name: String, var age: Int,note: ListBuffer[Note])
class Note(
  email: String,
  note:  Int)

var m = Map[Tuple3[Int,Int,Int],Person]()

如何更新映射以将新元素添加到listbuffer中。

使用所需的注释分别创建listbuffer。然后,只需按如下方式创建地图:

val lb = scala.collection.mutable.ListBuffer(new Note("s@s.com",1), new Note("d@d.com",2))
Map((1,2,3) -> new Person("samar",0,lb))

<>你应该考虑在Scala中使用Case< /Cord>类-它们免费提供给你很多好东西。假设您确实将类更改为案例类,则以下内容将实现您想要的:

import scala.collection.mutable.ListBuffer

case class Note(email: String, note:  Int)
case class Person(var name: String, var age: Int,note: ListBuffer[Note])


val n1 = Note("foo@gmail.com", 4)

val c1 = Person("John", 20, ListBuffer(n1))

val m = scala.collection.mutable.Map[(Int,Int,Int), Person]()

m += ((1,1,1) -> c1)

val n2 = Note("bar@gmail.com", 40)

m += ((1,1,1) -> c1.copy(note = c1.note += n2))

println(m)

res1: scala.collection.mutable.Map[(Int, Int, Int),Person] = Map((1,1,1) -> Person(John,20,ListBuffer(Note(foo@gmail.com,4), Note(bar@gmail.com,40))))

你可以试着去做,已经试过了,但是没有机会。我想要的只是一个向导!!对于我必须标准化的东西,因为我非常困惑,所以请告诉我们你已经测试了什么((1,1,1))=(注意=列表缓冲区(电子邮件=”test@gmail.com“,注=2))