14.2 Collection and Generics in Java | Practical

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens this is I've been ready from physical learnings and in this video we'll talk about collection and generics so collection and genrich's is very new to Java not paying you exactly but when jar was introduced the first version was 1.0 right and 1.0 collection was not there collection was introduced in 1.2 version and later in 1.5 we got one generics concept okay so now since you are working with Java 8 we have all the features which is collection and genrich's now why we need a collection it's because collection provides you a dynamic type of arrays as we have seen in the theory so it is a dynamic array where you can add your elements and you can expand the size of array you can reduce the size of added so that's the advantage of using collection 0 why we require Genrich that we'll see once we start with the session now let's say I want to add some values so we can add values inside a day right so we can have a similar type of elements inside array but why sips why similar types you can also have an array of objects right example let if I have an array of integer so if I say int values which in which is an array I will say new end I can specify size of 4 so that means in this array with which is values we will only have int values right we can also have a list of objects right so we can say object we can say values 1 it's an add a new object so it's not compulsory to have this similar type of elements right we can now we can have an array of objects here so let's say we have four objects in this four objects I can specify the integer value so I can say values of zero this is let's say my name naveen okay I can say values 1 of 1 we can say value 7 right so we can have integer we can have string we can even have an object of a student class right so we can have anything inside this object array so this array here it's the integer values and this added here these are objects in this object we can have any type right so these are similar type of elements which is object type and an object we can have anything right but the problem with this two addresses they are fixed that means we can have maximum four elements so we can start with zero we can go to one two and three maximum we can have four elements what if you want to increase the size of the array that's not possible can we decrease the size of array that's not possible right and that's why we go with something of this connection so what is collection collection is a interface in Java okay so let me input the package for this I can say control space I which will give me the suggestion which is collection Java util and if I say enter so you can see we got a collection here let us ignore this 'part so in order to create object of collection we can say collection again say values so let me comment all this part so that I can reuse the name which is values which is my favorite name so have a values here but the problem is this collection is an interface right so this is an interface but what we need we need a we need to we need to create object of this collection right so we cannot create our own class which will implement this so what we can do is there are certain classes which is available in collision framework so you can see collection is a framework so we have we used three terms with collection reuse collection as a framework we use collection as a quote as a class collections with your class and collection as an interface so this collection is the interface here okay so this is the collection which is a root interface for all the collision classes and interfaces so we have certain classes which implements the collection interface here so way to search for that class here now how to search that class so what we can do is we can just go to google okay and we'll search for the collection API okay we can see what are the classes available so when you say collection in Java there is the best place to learn the Java concept which is Oracle website so you can go to the order official Oracle website right we can you will find everything there no need to go to other websites to learn anything you can just directly go to this website and you can learn anything you want right so you can see the collection framework here you can see the implementation you can search for all the images like example we have this so we have a collection interface which is implemented by or which extended by this all interfaces if you can find the class implemented by any of this interfaces we will get our thing right so what we'll do we'll go to Google Images so that will get that go to Google Images which is you know and let's search for collection API in Java not now it's Java and you can see here we have a diagram it which explains everything the or this is awesome hand diagram so now we have a collection interface here and the collection interface extended by this all interfaces we have said we have least and we have Q and inside least we have at least vector and we have link list right so we can use any of this we can use the edges vector or list we can also use hash set we can use priority queue but this for this example we'll go for other dish we'll say new add a list here and we can provide the black kids so in this analyse let me input the package so once we got the object of analyst here which is so we are creating our interface reference which is values and then object of ArrayList let's add some values now how to add values here so we can simply say now we cannot use that square bracket here we have to since values and objects so we can say values dot add so that is a method of using bits you can add value so let's say we are adding three so we can add integer values I will say values dot add and this will add a let's say let me insert my name there so I will say Naveen and let me insert a float value also will say value start add why I am getting every time at all and this time as I will insert five point eight okay that's the double value right so if I mention five point eight F that will be float value so we can add elements in of any type so this is a collection of objects right so when I say what is collection so we can say collection is the collection of objects right and now how to fetch values so in this values collection we have added three back three elements now to blend all those values now it is very easy to paint these values we can simply say there are three ways you can fetch values the first way in fact we have two ways to fetch values from the collection the first way is using iterator so we can use me write it here so we have two ways one is using iterator and the second way is by using enhanced for-loop okay we cannot use that with help of normal for loop we can use enhanced for loop okay so we have two things we have a traitor and then we have enhanced for loop now how to use iterator here to fetch the values from the collection we need to use a traitor okay against the interface in Java so we can say iterated I equals u now to get the object of iterator since it's an interface we can call the collection so we can say values because this values will have a method which is iterator so this iterative method returns the object of iterator so if you can click on the situate or method of the collection class or the interface you can see we have it returns the object of iterator so we got the object here now once we got the object we can fetch the values using next or method now how to do that we'll say us out and we'll say I dot next so this is the method using which you can fetch the value okay so I dot next we'll give you the next value and if I run this code you can see we got three but if I want to print naveen also so I will say copy-paste and I want to print five point eight so I have to write this statement three times so we got those three values right so we need to use a traitor to fetch the value from the collection what the thing is a mission of adding this statement or three times can be use a for loop so can we say for loop which starts with I equal to 0 I less than equal to 2 that with that thing we can do right otherwise let's say if I don't know the number of values we have so we can use a normal for loop or the while while loop in which you have a method called as I and we'll say I dot so this is a method called as hasnext so this has next will give you it will check if if do you have the next in the collection okay so this has next we'll check for the next element if you have the element it will print it otherwise it will stop okay and we just try to write it once if I on this code you can see we got these three values right so that's how you use iterator with the collection but the problem is iterator is very alright so the way it's a very old technique can be using advanced technique here can we use enhanced for-loop so I want to use this enhanced for loop here now how to do that we can use a for loop here and how to use enhanced further we can search for a video on a nonce for loop in my playlist so in announce further we just mention the type of element so we'll say we have I and I is coming from value so we are fetching the values of I from the values but what is the type of I here so the type of I is object because in values you you don't have any specific type so we'll take object okay so the type of values is object so this object I will take one value from the values and we'll print R here and if I run this code we got the same output right so we can add some more values we can say value start add let's add a value which is a 77 right if I on this code now you can see we got four outputs so even if we do not know the number of elements tax okay we can use enhanced for loop to fetch all the elements now the next problem is I want to be very specific my collection should be of type integers it should not support string and float it should only support integers for that after the collection we can mention something in this angular bracket so we can mention that in this collection you should only have integers which not support end or you should not support a float and strength so I don't want to go with the object type I want to go with the integer type and we have to mention this on both the side okay and this concept of mentioning what type of data you have in your collection is collagen rigs so that's the generics here so this collection if you click on like collection it was introduced in 1.2 okay and you can see there is something written here so this a represent the type of element you are working with so let's say if you have all integer values so we can have all we can write integer here so let's say if you are working with float so you will be having float here we can also have the collection of students so you have to mention student here now what is student you can create a class a custom class in which you can have student student parameters properties and methods so you can mention student here so you can see we are getting edit in these two lines is because it will not support it now you can only have you can have only have integers here so again I will say value start add and here will 88 so now we have three elements here we have three 77 and 88 so we have three values both online integers and if on this code we are still getting the same output right but hold on in the fourth loafie writing object why we need object there when we know that the elements are integer so we can use integer or we can even use int and that's the awesomeness about the wrapper classes this is boxing unboxing even if you have all the integer values we can fetch using int and if I got this code we got the output right so we can write integer we can write int there so that's how you work with the enhanced for-loop with the collection api sounds good so this is collection and genrich's we can have any type of elements here so we can use iterator to fetch the values we can use for loop now there is a small problem let's say I want to add the element in between so I want to add the element after three so let's say if I have a element which is values dot add I want to add the element let's say nine in between after three and before 77 so if I add nine and if I then this code you can see the nine will be coming up to 88 but I want to print nine after three how to do that can be specify the index number can be say it will be a in this way it should be at index one because three zero so I can say index one and nine and the answer is no because there is an error it says you don't have any add method which takes two parameters because if you go to collection interface you do not have any method which is the name is add and you get takes two parameters so you can see we just have one add method which takes only one parameter and that's the issue right so how to do that that we'll see in the next video where we will talk about list interface because list is the interface which extends collection it has some extra features and you guessed it right it will be having a feature where you can have you can add the elements with the particular index number sounds good so we'll see that in the next video in the in this video we are the mini well with one more thing we can also remove the element so let's say if I want to remove 77 we can do that or we can simply say values dot or let me remove this L so we can say values dot remove and we can mention 77 there and if I than this code we get only 3 and 88 simple so that's how we can use collection genrich's with an answer for loop an iterator so thanks so much for watching and do subscribe for further videos and in the next we will talk about list
Info
Channel: Telusko
Views: 228,782
Rating: 4.9209342 out of 5
Keywords: what is collection in java, generics in java, What is collection interface, how to implement Arralist class in java, enhanced for loop, using Arraylist, what is iterator interface, iterartor, how to implement enhanced for loop in java example, navin reddy, naveen, telusko learnings, java tutorials, java for beginners, collection, generics, basic, simple, java, telusko, what is, how to
Id: HhrQqmp3hXI
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Wed Apr 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.