我想根据scala
中的参数过滤列表 case class Student(
name:String,
age:Int,
subjects:List[Subject]
)
case class Subject(name:String,id:Int)
val sub1=Subject("maths",101)
val sub2=Subject("science",102)
val sub3=Subject("english",103)
val s1=Student("abc",20,List(sub1,sub2))
val s2=Student("def",30,List(sub3,sub1))
val s3=Student("XYZ",40,List(sub3,sub2))
val sList=List(s1,s2)
def findSubjectId(sList: List[Student], subject:String) {
sList.map(student => student.copy(subjects =
student.subjects.filter(_.name == subject)))
}
参数2 - 年龄20 OR 30
结果 - 应该是可选的
答案 0 :(得分:2)
你可以试试这个:
sList
.map(student => student.copy(
subjects = student.subjects
.filter(_.name == subject)))
.filter(student => student.age == 20 || student.age == 30)