假設你有一個像下面這樣的案例類
case class Fruit(name: String, color: String, price: Double){
}
你還有案例類串列
val Fruits = List
(Fruit("Apple", "red", 3.00), Fruit ("Banana", "yellow", 4.99))
How do you filter based on name?
uj5u.com熱心網友回復:
使用filter函式選擇namecase類的屬性Fruit
scala> Fruits.filter(fruit => fruit.name == "Apple")
val res0: List[Fruit] = List(Fruit(Apple,red,3.0))
uj5u.com熱心網友回復:
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/377864.html
