Splits the original array / collection into pair of lists, where first list contains elements for which predicate yielded
true
, while second list contains elements for which predicate yielded false
. Please refer the below Code Snippet for the details.
val employeeList = listOf( EmployeeInfo("HB100", "Ragavan A", 30), EmployeeInfo("AX782", "Peter J", 54), EmployeeInfo("ZD242", "Karlos V", 56), EmployeeInfo("CD902", "Ismail N", 24) ) val (ageAbove50, ageBelow50) = employeeList.partition { it.eAge > 50 } println("\nEmployee List: $employeeList") println("\nAge Above 50: $ageAbove50") println("\nAge Below 50: $ageBelow50") /*Since it returns as pairs, If we want to ignore the other condition i.e. ageBelow50 employees use _*/ val (ageAbove50Plus, _) = employeeList.partition { it.eAge > 50 } println("\nAge 50+: $ageAbove50Plus")
Output:
Employee List: [EmployeeInfo(eId=HB100, eName=Ragavan A, eAge=30), EmployeeInfo(eId=AX782, eName=Peter J, eAge=54), EmployeeInfo(eId=ZD242, eName=Karlos V, eAge=56), EmployeeInfo(eId=CD902, eName=Ismail N, eAge=24)] Age Above 50: [EmployeeInfo(eId=AX782, eName=Peter J, eAge=54), EmployeeInfo(eId=ZD242, eName=Karlos V, eAge=56)] Age Below 50: [EmployeeInfo(eId=HB100, eName=Ragavan A, eAge=30), EmployeeInfo(eId=CD902, eName=Ismail N, eAge=24)] Age 50+: [EmployeeInfo(eId=AX782, eName=Peter J, eAge=54), EmployeeInfo(eId=ZD242, eName=Karlos V, eAge=56)]