我有一个包含此类中元素的列表:
case class DiningRecord(
name: String,
cuisine: String,
address: String,
timeOfVisit: String)
现在我需要生成一个新列表或者通过烹饪字符串替换原始列表中的元素。有什么办法可以吗?我是Scala的新手。
答案 0 :(得分:0)
如果要替换数据类中的字段,请使用.copy
并更改要更改的值。
参见REPL示例,
scala> case class DiningRecord(name: String, cuisine: String, address: String, timeOfVisit: String)
scala> List(DiningRecord(name = "name1",
cuisine = "cuisine1",
address = "some address",
timeOfVisit = "2017"),
DiningRecord(name = "name2",
cuisine = "cuisine2",
address = "some address",
timeOfVisit = "2018")).map(_.copy(cuisine = "new cuisine"))
res10: List[DiningRecord] = List(DiningRecord(name1,new cuisine,some address,2017),
DiningRecord(name2,new cuisine,some address,2018))
上面的示例创建了一个新的数据列表,其中的菜肴为new-cuisine