I have a list of a case class. I want to get a particular item from the list.
I do
myList.filter(_.id == myobject.id)(0)
This would work when the filter actually returns something. But when filter doesn't return anything I get an index out of bound exception.
scala> case class Color (id: Int, name: String)
defined class Color
scala> val myList1 = List[Color](Color(1, "red"), Color(2, "green"), Color(3, "blue"))
myList1: List[Color] = List(Color(1,red), Color(2,green), Color(3,blue))
scala> val toFind1 = Color(10, "white")
toFind1: Color = Color(10,white)
scala> myList1.filter(_.id == toFind1.id)(0)
java.lang.IndexOutOfBoundsException: 0