scala - Filter a list based on two parameters -
i want filter list based on parameter in 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))) }
- parameter 1 - subject
parameter 2 - age 20 or 30
result- should optional
you can try this:
slist .map(student => student.copy( subjects = student.subjects .filter(_.name == subject))) .filter(student => student.age == 20 || student.age == 30)
Comments
Post a Comment