假设您有一个如下所示的案例类
case class Fruit(name: String, color: String, price: Double){
}
Run Code Online (Sandbox Code Playgroud)
你还有案例类别列表
val Fruits = List
(Fruit("Apple", "red", 3.00), Fruit ("Banana", "yellow", 4.99))
How do you filter based on name?
Run Code Online (Sandbox Code Playgroud)
使用filter函数选择name案例类的属性Fruit
scala> Fruits.filter(fruit => fruit.name == "Apple")
val res0: List[Fruit] = List(Fruit(Apple,red,3.0))
Run Code Online (Sandbox Code Playgroud)
List有一个filter方法。
case class Fruit(name: String, color: String, price: Double)
val Fruits = List(Fruit("Apple", "red", 3.00), Fruit ("Banana", "yellow", 4.99))
Fruits.filter(_.name == "Apple")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
334 次 |
| 最近记录: |