Hello All,
Today I am going to share 2 another important operations that are "filter" & "map" ,
Filter : This is used to apply a condition , on collections elements and get the resultant collections data , then there "filter" is used.
Map : This is used to transform all the elements of collections data
Output :
Here , we can see that I have added the condition to get even number from the myList , which is returning myEvenList , then I applied the map condition for getting the double numbers of all the elements of myEvenList , which we are getting in myDoubleList .
Now take one example of Employee Data class :
Output :
Here I have used one list , which is containing the list of Employees ,
then in nameList , all the names are fetched using map()
then from nameList , all the names are fetched starting with 'A' , returning the initialList .
Hope this will help to understand these two important topics of Kotlin. Will share another interesting topic in my next post , till then Happy Coding 😀😀
Today I am going to share 2 another important operations that are "filter" & "map" ,
Filter : This is used to apply a condition , on collections elements and get the resultant collections data , then there "filter" is used.
Map : This is used to transform all the elements of collections data
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var myList = mutableListOf<Int>(34 , 6 , 29 , 15 , 65 , 9 , 12 , 42) var myEvenList = myList.filter { it%2 == 0 } for(num in myEvenList){ println(num) } println("Double list : ") var myDoubleList = myEvenList.map { it*2 } for(num in myDoubleList){ println(num) } }
Output :
34 6 12 42 Double list : 68 12 24 84
Here , we can see that I have added the condition to get even number from the myList , which is returning myEvenList , then I applied the map condition for getting the double numbers of all the elements of myEvenList , which we are getting in myDoubleList .
Now take one example of Employee Data class :
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var myList = listOf<Employee>( Employee(101,"Alen"), Employee(102,"Harrey"), Employee(103,"John"), Employee(104,"Sam"), Employee(105,"Alice")) var nameList: List<String> = myList.map{it.empName} for(name in nameList){ println(name) } println("Initial List : ") var initialList = nameList.filter{ person -> person.startsWith("A")} for(num in initialList){ println(num) } } class Employee (var empId: Int, var empName: String) { }
Output :
Alen Harrey John Sam Alice Initial List : Alen Alice
Here I have used one list , which is containing the list of Employees ,
then in nameList , all the names are fetched using map()
then from nameList , all the names are fetched starting with 'A' , returning the initialList .
Hope this will help to understand these two important topics of Kotlin. Will share another interesting topic in my next post , till then Happy Coding 😀😀
No comments:
Post a Comment