Kotlin Generics Explained with Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone in this video we will learn about  generics and Kotlin generic is a powerful feature   that you can use to define classes or functions  that can work with different data types but before   moving into generics let's try to understand why  it is needed so for this example I will define   a simple class so let's say we have a class  that is arrayutil and this class accepts   an array in its constructor so here I will write  private val array and it is an array of type end   now inside this class I will define a function  that will search a given element inside this   array so let's define a function let's say we  have fun find element and inside this function   or to the parameters for this function the first  thing that we will pass is the element that we   need to search so we have element of type end and  the next parameter is a function that we will call   when the element is found or not found so I will  define it as found element now for this function I   will define two parameters the first one is the  index of the found element so we have index of   type end and the next parameter is the element  itself so we have element of type and again   and I will make this next parameter as nullable  because I want to pass null when the element is   not found and finally for the return type I will  define unit like this so we have a function that   will search for a given element inside this array  now let's search for the element and it is very   simple we can simply run a loop so let's say we  have for i in array dot indices like this now   here we will check if array i is equals to equals  to the given element that means the element found   and in this case we will call found element and  we will pass the index that is I and the element   like this and we will break the loop with the  help of reader and in case the element not found   we will do the same thing but for the index  I will return minus 1 and for the item i   will return null that means the element not  found now this code will work so let's test it   so i have val array util and then array util and  to the constructor we need to pass an array of   type end so here we can pass array off and let's  say I will pass one two three four five and then   I need to find let's say I need to find three  so I will write array util dot find element and   the first parameter is the element that I want to  find and the next parameter is a lambda so here I   can pass a trailing lambda so let's say we have  this lambda and this lambda will have the index   and the element like this and here  we can print index and element. Like this now if i will run this code it should  work so let's see if it is working or not   so you can see the code is working the  index is 2 that is 0 1 2 and the element s3   but the problem with this code is it will  work only with array of type end and if we   want to make it work with different data types  we can create different classes but it is not   a good solution and to fix this problem we have  generics now to this class arrayutil we can define   types so with the help of these angular braces  you can define types for example let's say we have   type T here and you can also define multiple types  like t x y you can use any character for defining   a type so i am defining here type T because at  this moment I need only one typed parameter so   this is called a typed parameter and T means the  type as not defined and we will define this type   while creating the instance of the class that  means we will define the type here so this is   a class that is accepting a typed parameter and  when we will define an instance for this class   as we are defining here what we can do is  we can define the actual type so let's say   we want end for this case so now whenever  we will define an instance for this class   we can define different types for this T for  example we have end and now we can have string now   for this instance the type for this T is string  and for this instance the type for this T is end   so now this class will work with any type so let's  fix this code so here instead of end we can write   T so we have an array of type t now here for the  function find element instead of end we will write   tTlike this now we will pass an element of type  T and index will be end only but the element here   will be T that's it now the code will work  everything is fine now this code will work for   any type for example we used it for end arrays  and we can use it for string arrays as well. You can see it is working absolutely fine now the  same class will work with any data type because   type here is not defined and we will define  this type while creating the instance like this   now you can have multiple parameters like this no  problem and you can define multiple types as well   let's say we have end and string so it is for your  requirement you can define as many types as you   want for now I will remove the extra type because  only one is required now and you can define the   types for the functions as well let's say I will  remove this class so let's copy this function   and I will comment out this class like  this and let's say we just have a function   that is find element so for the function  we can define the type like this. And to this function only we can pass the  array of type T like this now the function   is same we just need to pass the array to the  function directly so I will delete this thing   and here I will call find element and  for the first parameter i will pass array   for the next parameter the element that I want to  find and the last parameter is the trailing lambda   that's it I will delete  everything and it will work now. You can see it is working absolutely fine  so you can have typed parameters for your   classes and your functions and you can also have  multiple typed parameters for example we have fun   let's say we have x and y two typed parameters  and i will write the function name as   just for testing or you can write whatever you  want and here i have param1 of type x and param 2   of type y and here simply I will print. Param 1 and param 2 like this now whenever I  want to call this function I can call it like   this just for testing and here I can define the  type for example I have string and end so I can   pass 3 and I can pass 4 string and end now if I  will run the code I will get param1 and param 2.   You can see it is working absolutely fine so  this is how you can use generics to define   classes and functions that will work with  different data types so that is all for this   video friends in case you have any question  you can leave it on the comment section below   thanks for watching everyone this  is bilal khan now signing off.
Info
Channel: Simplified Coding
Views: 6,409
Rating: 4.9103141 out of 5
Keywords: kotlin android tutorial, android kotlin tutorial, kotlin generics, generics kotlin, kotlin typed parameters, kotlin tutorial for beginners, kotlin oop, oop kotlin, kotlin object oriented programming, android development
Id: yuJNXSZFET8
Channel Id: undefined
Length: 10min 18sec (618 seconds)
Published: Wed Jan 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.