Collection and Generics in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we'll come back alias this is I mean ready and in this video we'll talk about collection and genrich's so what exactly collection means so when I say collection this in the whole video of you on the whole series I'm talking about three different concepts so one collection so if I say small C so if I say collection so this is a topic this is a concept okay so when I say we have this in collections and we have this in collection I am talking about the concept then we have capital C which is collection it's an interface so when you when you say you want to provide an interface you want to create a collection of elements that will be yours we're talking about capital C and then we have one more which is collections so this is s so in the end we have s that's a class so this is a concept so we are talking about the collection concept now then we have collection which is the interface and then we have collections which is a class okay so now in the whole series we were talking about collection API we have genrich's so so we try to merge genrich's inside collection so we have collections here with collection we have added lists we have said we have hash map we have map we have comparator we have comparable will be looking everything in detail in this series this video is just the introduction for all this we'll talk about why we require election API why we require all this different genrich's how to mention wrapper class there and all those stuff right so let's start with the first thing so let's say this okay now if I talk about the collection API so in that you have lots of classes you have lots of interfaces right so let's say we have the first one I want to I want to have the list of elements so I want to have at least five elements in the list so when you want to have five elements so the first thing comes to your mind will be in added right so you have to create an array so let's say we have I want list of integers I will say int I will mention a bracket and I will say this is new Android that's how you create an array and then you have to mention a size here which I mentioned for values so now we have an array of four elements now the problem with this is the size of this array is fixed now you cannot you not expanded you cannot shrink it this size is fixed right and we don't want the size should be fixed I want a array of I need something which will be expandable so let's say initially I want only four value so it will be four in future if I want five six values the the object should expand and that's why instead of walking with paint array we should work with collection now how to create a create a collection here so the same thing here okay with with adding we will try to make it collection so how to make it collection we can say this is collection okay that's the interface here collection interface okay which capital C and I will say this is values now we don't have to mention any square bracket there we can just mention values and we can say equal to so it is collection values equal to now the problem is we don't have so Croatia is a class so the collection interface white we cannot gate object of interface so that's why we have to search for the implementation we have to search for an implementation which implements collection and that thing here is so we'll say new add a list so what is a deduced to you so added list is a class which implements collection indirectly because analyst implements list which is interface and least extends collection so hierarchy goes in this way so we have collection at the top and then we have list which is again interface so this is interface this is interface okay and then we have added list now this thing here it is a plus okay so we have a class at least which extends which implements list and this list extends collection which is the interface okay now once we caught the collection we can add values now how to add values we can simply say it is values dot add and then so that is a method which using which you can add elements and then we can say this is five okay and then we can add let's say one more value so we'll say values dot add we can say six and then we can add four elements right the amazing thing is the size is not fixed you can expanded you can shrink it so if we did let's say we have 50 elements here and you want to delete the 23rd element you can do that and automate the a delete operation will shrink awesome right so instead of using a days we can use connection sounds good so from today will not use a days will only use correction Holden does not the case the thing is if we talk about the fixed number of elements so if you know they're in the number of elements is fixed always go for a day because a day is a days fast compared to collection so if you know this size will be fixed go for a D if you if you think this size is not fixed then you can go for collection where you have the option of expanding and shrinking sounds good now the question arise when you say you have an added here so this the type of this array is eight right you are mentioning paint here so the type of a is end what about this values we have not mentioned any type here so by default the type here will be object type okay now what I mean by that so you can in this collection you can add any values you can add numbers you can add string you can add different objects right so we can simply say it is values dot add we can even we can even add a name here so we can say this is Naveen so we can add numbers we can add string we can add all different types of values right now I don't want that I want to be very specific if I were if I am working with values so I want only integer values I want only float values I want only student type of values so how to do that then we have to use something like generics to specify this we have to mention generics how to do that so let me just put some space here I will say this is collection so that's my interface now how to mention that I am working with what type of elements so instead of after this collection we have to mention that we are working with integers okay now this line will give you an error because we have the collection of integers so this will so we can enter 5 we can enter 6 but we cannot enter now we here because that's a string right and then to specify the genrich's on this side also so we have to mention angular brackets we have to say integer and then so that's how we can mention integer there so this collection will only have integers now okay now why this angular bracket is because all different types of brackets they are booked right we cannot use round brackets because that is for function or methods we cannot use curly brackets but because that is for definitions method definition class definitions we cannot use square bracket because that is for array so the only thing which is remaining now is angular bracket so that is why they have used angular brackets for generics okay so you can have a collection of integers you can have collection of string you can have collection of student objects so just mention the type here so in 1.2 we got collection API in 1.5 we got generics okay so generic was there or gendering is there from 1.5 in 1.4 1.3 generics was not there it was only collection and then in 1.7 we got one more update which simply says if you have integer on this side why to mention integer on that side it is it is it is we can assume that right on the left side if it is integer on the right side will be also having integer so I need to mention that what you have to mention the diamond symbol then and just give the brackets and this thing only supports so this type of signature or the supports in 1.7 and above okay so that's how we work with collection and general X here again we'll see the detailed description or detail walking off bit in the subsequent tutorials so there's just a theory of that so that we will understand the practical implementation where to use it how to use it how to implement that we'll see in the practical session but before that I have one more thing here so if you talk about collection everything is walking here right you can add elements you can delete elements oh can you delete elements the problem is so let's say I want to add elements in between so after five before six I want to add a line element which let's say 8:00 so if I say values dot ad and if I say this is 8 here now this thing will not work also it will work but the sequence will be 5 6 & 8 I want to add 8 after 5 now the problem is we cannot even specify the index number because collection doesn't work with index number that's the problem right so in order to work with index numbers instead of using collection what will do is will not use collection here will use list so you can see least implements or extends collection it has some extra features that least works within index numbers okay so we have collection we have list and we can create object of ArrayList we want to work with list or to work with collection right now the next thing is we have we have something called as set so it's not fusing instead of using list we can also use set so we can say set of integers and we can specify let's say numbers and we can say new now the problem is this set is an interface again we have to search for the class which implements set and the class name is has set and we can mention the angular brackets here okay so we have set and has set here now the advantage of using set instead of using list is in least you can have duplicate values in set you cannot have duplicate values so every element in set will be unique so if you want only a collection of elements those those elements should be unique goes go for set if you want to look at values you can go for list which even upon you or depend upon your requirement with which on which you want to work with so maybe you want to walk with list of elements or which is duplicate elements you can go for list if you want unique elements go for set here ok now so we have has then too the problem is when you work with list you will be having unique elements right of course so you will be having two bigger elements and you can traverse in say so you can start with the first element to the last element so the order in which you will get the elements is fixed what about set the order in which will be getting the elements is random again you will see that in this in the video and the practical video so once you work with has said if you add the elements if you plaintiff you fetch all the elements you will get you a different sequence right so that is set here so instead of using has set we can also use preset so we have has said we also have preset now the advantage of preset it will give you everything in a sorted format so let's say if you add elements like five six a two three nine so if you add this elements the output you will be getting when you fetch values from this set you will be getting two three five six eight and nine so you will get this in alphabets in solid format so that's the advantage of using preset so we have lots of classes we have lots of interfaces in one big package which is collection API okay and the last point now so we also have something called as math now the advantage of map here so let's say if you talk about least inside least what you can have is you can have duplicate elements and every element will have a unique index number but what about map here now the advantage of map is map will not have a number it will have a key so it will be a key value relationship which means for every value you will have a key example so let's say if I have a value like phone number so the phone number as pH and Oh will be will be a key right let's say you have your maybe if you are working with in advanced part maybe in we've talked about some of the JSP you have to add labels audio you want to ad lib to the you want to add values to the session you can again use key value pair so every value will have a key the key can be a number it can be a string it can be anything right so how to get object of this so we can let's say my key is integer and my value is a string so I will say string now the problem is so we said this is M the problem is a map is also interface now we have to search for that class which implements that interface right and that class is hash map okay and then you have to provide the brackets so we have two class here which implements map one is hash map and second is hash table one is synchronized one is not so it's all depend up on you which one you want to use so you you can go for a thread safe implementation or you can go for a non thread safe implementation okay so it's all depend up on your requirements so you can use list you can use set you can use collection you can use map hash map hash table and all this part belongs to one packet which is again collection API so to understand this how all this thing works you have to watch my videos which is practical videos and if you have any doubts if you have any doubts inside this so make sure you comment in the comment area and if you have liked this video just click on that like button there and that's it so make sure subscribe the channel so that you will watch the further videos
Info
Channel: Telusko
Views: 588,162
Rating: 4.9480338 out of 5
Keywords: Collection framework in java, Collection in Java, Collection Framework by Navin Reddy, Collection Framework by Naveen Reddy, Collection and Generics, by Navin Reddy, by Telusko Learnings, ArrayList in Java, list in java, Java Collections, Java Map collection, navin reddy, telusko, Java Tutorial for Beginner, collection, Collections, Collection, difference between, what is, how to, interface, class, concept
Id: 5NOLuoG6fcQ
Channel Id: undefined
Length: 14min 31sec (871 seconds)
Published: Mon Mar 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.