Java Collections | Collections Framework in Java | Java Tutorial For Beginners | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends very bomb welcome this is a shunt on behalf of Eddie Rica so guys this time we will be working with collections framework as in our tutorial so let's see what and all we gonna cover father day so you'll understand what is javis collection framework we'll see the hierarchy or the relationships between various EBI is in the collections framework we'll have a quick introduction to interfaces because in collections framework we have many interfaces we'll see a demonstration on list queue and set for the day so these are the three different data structures as well so how we gonna structure the data in list UN set so there are fundamental classes with the help of which we can create these data structures so we'll see now how we can implement collections framework in our java applications so to begin with the Java collections framework guys so collections they are the containers that are going to group multiple items as a single unit you can consider them as a multi value container so they are dynamic containers so you can add remove data dynamically that is at runtime so collections framework is an architecture where we can store and manipulate the group of objects so if you are implementing Java collections right so we can use the building functionalities to search the data sort the data you see a manipulation like insertion or deletion so we can do lot of operations which are building for us so the traditional data structures you need to write the epa's yourself but here you get the methods which can do the tasks for you and that took effectively and efficiently so Java frame work will provide us interfaces and classes in order to you know store the data so Java collections framework hierarchy is set of API is linked to each other as parent-child relationships so now let us try to explore that so collection framework hierarchy is going to start with an interface called atrial so it raver is the root interface so from where this collection if you see is inherited and from the collection we got list Q and say so all these are interfaces now from the list we have three fundamental classes ArrayList linked list and vector yeah from the vector guys we do have one more implementation called stack so from the queue what we have is a priority queue so this sorts the data in the queue as well and from queue we got beat you that's like double ended queue and we got a fundamental class called array DQ so in the set we got hash set and linked hash say so in the linking here so guys this is implements so this arrow over here the dotted arrow it says implementation so these classes they implement and this solid are over here it will tell us about extension right so interface can inherit another interface so here stack is inherited from vector that is the collections framework hierarchy so we even have this sorted set called tree set which will sort the data for us now the way we got this list queue and say as the data structures we do have something known as map so guys in the map we have hash map and hash table so we do have a sorted version of the map way we get pre map but for the today's discussion we gotta focus on list you and set so what are in Java so guys it interfaces in Java they are the rule books so there you write the methods which must be implemented by the Java objects as runes so interfaces they are the reference types where they are similar to classes but contains only the abstract methods as rules so we got interfaces they cannot be instantiated neither you nor the runtime environment can create its object interfaces cannot create constructor or fields they can contain only abstract methods so if you write a class which is the structure of an object so you can implement an interface right so implementation is just like let's say traffic police they have rules now so do not cross the red light that's a rule vir the seedbed when you are driving the vehicle that's a rule so these rules are supposed to be defined by us right so beer a helmet in case you are driving a two-wheeler so this is a rule now but the definition of this rule I have to define that whether I want a full face helmet or partial covering the helmet a red color helmet or a black color helmet so that has to be undefined money so rules are implemented so interface can extend multiple interfaces so we can create rule books from the existing rule routes so once we know interface so now coming back to the collections framework so guys iterator is one interface so iterator interface in the collection frameworks will provide the facility of iterating the elements in a forward direction so once you have a data structure for example let's say list you got so many elements and now you are a trait in this list so we got iterator which can help us to iterate in the forward direction so they are methods hasnext next and which we can use that the table as an interface is a root interface to all the collection classes we just checked it in our previous slide right the collection interface along with all its subclasses will also implement the it really interface so this interface in Trimble is a root interface so everyone implements it so this guy will give us a method called iterated through which we will get the reference of the iterator and we can iterate in a forward direction in any data structure collection is also an interface which is implemented by all the classes in the collection framework so as to achieve runtime polymorphism so the methods like add and all clear and many more methods the similar methods you will find in various api's of the collections framework now we'll start with the data structures or the collections our first topic called lists so guys what is a list so list is something which extends collections framework and it stores the animates in an indexed approach and we can even add duplicate element so list supports redundancy so there are three types of Lists right we got ArrayList linked list and vector so there is ArrayList linked list and vector they are the fundamental classes which implements list interface alright so what are these three classes and what is the difference between them so ArrayList is implementation of list interface where we can dynamically add or remove the data so here what happens the data which is added is added in an indexed approach so we got indexing for the elements which you store in the RLS right so the next element will be added on the index X if you have already 0 to 5 occupied with some elements so on and so forth this array list will go guys till the end so if you want to create an ArrayList you need to write an ArrayList object by saying a new array list and ArrayList class is going to have various methods for example add to add any element we got cleared which will clear all the elements from the ArrayList we got index of last index of two array trim to size many more methods in the same class ArrayList so this belongs to the Java dot util package and it implements the list interface so the way we got ArrayList we have linked lists so linked list is a sequence of links so the data storage or the data structure here we have reference to the other length so we create a linked list by saying linked list object is a new linked list and there are two different types of linked lists we can implement as in a singly linked list or a doubly linked list so what are the difference now in a singly linked list each node in the list will store the data of the node and the pointer or a reference to the next node so let us have an you know view here so we'll begin from the head which will be pointing to a node so node can have the data and the reference to the next node and we keep on moving till we get the null in the end doubly linked list is where we have two references so what as in the next node other one as in the previous node right so node contains the data next node points to the next node and previous as a pointer or a reference will point to the previous four so that's like how we got a doubly linked list we ever have more versions to the linked list but these are the two versions available here so the various methods in the linked list so guys they will have common methods so all the you know these classes ArrayList linked list and vector they will have all the common methods now vector is the third you know API which implements the list interface so this is again a class which is similar to ArrayList the difference is that it is synchronized it means it is thread safe so our Ellison vector they are exactly same so even they store the data in the same way in the index approach right and the difference is that vector is a synchronized version of our Ennis it's a thread safe version of ArrayList so nice vector will also have the similar methods so now what I'm going to do is I'm going to demonstrate you guys what is an ArrayList over a practical example so what I'm going to do is I'm going to write a new Java project so let's see a new Java project so I leave it as collections so in the SRC I'll do right click and see a new class and I'm going to label it as list demo with the main method let us write the package code or edwiga so how are we going to create a list so we will say ArrayList from the Java dot util package so you get mentioned the type of data which you wanna store for example here I have mentioned the type of data which we are going to store is string so list one can only store string objects so whereas if you want you can remove this type so this becomes more of generic now in a way that you can store any type of data so list two can store any type of object so let us see how so the first operation is add data in list so I can say this one dot add so use an a third act so we'll add John that's a some more records Jenny Jim Jack Joe so I am storing this John twice so you see duplicate see now I won't be to store any other type here so I won't be able to store a number 10 I wouldn't be able to store a number 2.2 so this will be added so why we will not be able to store these values because this one over here is strictly a string data type so data which is being added is indexed one we got two three four and five so data which ever you are adding in the list is indexed now in the list too you can add any type of data so you can add John you can add number ten you can add 2.2 so considering that we have a class called student right so let's say there is a rule number and a name so I have these student objects student s1 is a new student where s ones rule number is 1 1 + s1 name is let's say Mike so you can even come here and say this - dot add s1 so in the list we can add any type of data right so here we are type specific so where we can add any type of data so now let us try to come here and print reference to list so I'll say printout list one is + list 1 and same way printing out list 2 is + list - so run this coding as java application so what you see is the first list contains John Jerry Jim Jack John and Joe whereas the second list contains John 10 2.2 2.2 so I think we are it this 2.2 twice so what I can do is I can add a character here let's say an e so we got this hash code for the student of right when we add s 1 so it is printing me the hashcode so this is printing all the elements in the list printing reference to the list shall print elements in the list the way we added in a sequence now what if you want to get the element from list so guys I will say string name is list 1 dot get on the basis of 2 so on the index 2 we got Jim right so I'll say C so name is last the name same way if you want to get the element from list tube so I can say object who is list 2 dot get on the basis of 2 now why I am using object because object is the parent of any type in Java so as per polymorphism parent can point to any object so let us say C so o is plus 4 so we don't know the type here right so there is no specific type so that's why we have kept the object here so when I run this code so you get to see the data so that is how we can get the element from the list so there can be an operation called update element English so as I can say list 1 dot set the second index now is let's see Fiona and if you now reprint the list 1 this one now is plus list 1 the second index so that like 0 1 & 2 is now updated with Fiona the previous value was Jane so Jim abated - Fiona so if you want to remove the element so you just say list 1 dot remove the number 2 and now you see least one after delete is or after remove is this one so this was the previous list with Fiona and now in the new list you can see that Fiona is gone if you want to just remove all you can simply say clear which is going to remove all so there are you know methods which can help us to perform searching and sorting as well so let me give you an example so if this one dot contains John so I'm going to come here and say John is in the list so contains is going to do a search for me it says John is in the list so you see we got api's to perform the actions of our choice we need not to write the code our say-so APA is going to do the job for us now the next operation is where we gonna iterate in rnase so how you gonna trade guys so you the foreman so there are various videos to do it but i'll say for so i'm going to use and has to four string STR in list 1 i simply say print out the STR so you're on the code array you get to see the entire data so I'm just gonna say C so it's rating babe enhanced for-loop so here we are now when you don the code so iterating Miller has for loop so you can even iterate with a basic for me right so a basic for loop she'll be like this so you will start from 0 you go till list 1 dot size and I plus plus so size method will give you the size so I'll just say list 1 dot get on the basis of I he trading me a basic for me so twice is yours so whatever the way you wanna eat rate you can be traded so on the basis of indexes or using enhanced for-loop now there's an API called iterator so using an iterator API we are going to break so iterating with iterator so let's see how so you will say it Ritter of type string let's say idea is list 1 dot iterator so iterator method in the ArrayList will give us a reference to the iterator and now I can simply say print out ITR dot next so I'll print out idea dot next two times now when I will run this code so you see it gives me John and Jenny so when you do ITR dot next again so it's gonna give me John Jenny and Jack so next method gives me the next element so it's not like iterating in a loop directly so iterator is going to give me the elements one by one whenever I call the method next so if you wanna use iterator and read all the elements I can go and check wild idea dot has next so I'll say string STR is i ER dot next and we'll print out the same as tia so this way we are going to iterate in the entire array list using imperator in a loop if you don't want to use loop you just keep on going one by one so it later can also remove the element so I say if STR dot equals let's say Jack so I'll say ITR dot remove and lastly so now when I will say list one after iteration is plus list one you run this code here so what you see is we got this guy Jack but from the list Jack is removed so we can use remove map third in the Prater API to even and believe the element from the array less now the way you got this rlist implementation in the same way we have lateness and vector right so this is a quick introduction to how you can use arenas so now the next data structure on our list is Q so guys Java Q over here is supposed to work on first-in first-out approach and other than this it's gonna sort the data for us so Java U is working on first in first out so the anywhere which is and it first will be removed first and the last element will be removed in the last so we can use Q and now we will have one of the implementations of the queue so guys so we got front and the rear so front will be removed first and rear will be removed the end so we have various methods again available in the queue so we got add remove we got peeking and pooling right so we'll see how we can not do this speaking and pulling in the queue so let's have one in production practical introduction to the queue so I'll write one more Java class so let us say this is Q demo with the main method so to create a queue I'll create a class called priority so we got priority queue and I will take integer numbers as the elements in the queue so this is priority queue guys right and what I'm going to do is I'll create a loop which starts from 10 and go still greater than zero and we'll keep on ticking ending the loop so here I will say Q dot add I so how we are adding the data over here so we are adding the data in the queue so let me show you so Q is like this 10 9 8 7 6 5 4 3 2 and 1 which means that 10 is added first right so this guy is the front and bonnie is added in the end so this guy becomes the rear so 10 is added firstly and 1 is added nothing but now priority queue sorts the data for us so priority queue will be sorting the data for us now when priority queue will sort the data so priority queue will make the queue to go like this so when the sorting will happen so 1 will become front and 10 will become rare so this is the actual queue right so this is sorted queue the actual one now there are two major operation so the first operation is speaking so peeking is obtaining the head of queue and the second operation is pooling which is we are going to so here we will remove the head of Q so let me come here and say print me Q dot P so what is this speaking so peaking is head of you and who is the end of queue guys the end of Q is the number one because the queue is sloppy so now when you run the code here as java application what you see is the number 1 now what I'll do is I'll come here and see let us do a pole operation so what is spoon whole is removing ahead right so pole will remove the head so in the next instruction if I'll do the peeking so the new head of the queue is 2 so removing the head 1 so now when you run the code you see that it is giving me 2 so that's like the new head so if I come here and yeah so in the same example so I'll just say huge size is plus Q dot size and once you to a poor operation so Q size after Polys you got signs so when you run this code here you see that the initial size was 10 and the element is 1 so I can just try to be more specific here so head of you is plus this same way I'm going to do like this so after the pole the size remade 9 and who is pulled head is removed and the new head of the Q is 2 so that is what we got peeking and pooling in the queue so guys Q is first in first out so what has happened over here so whatever the element you added in the front that will be removed first so that is one quick introduction to priority Q which even sorts the data for us now the next data structure is set in Java so what is a set guys right set is again a collection but this focuses on uniqueness so in the list we can add duplicate elements but here in the set we cannot add duplicate elements all right so guys said we'll focus on you know uniqueness so it's gonna use hashing technique by which we will be having unique elements coming in so it is mainly to model the mathematical set abstraction now set as various classes so we got three fundamental structures for the set we got hash set link hash set and the tree said now what is hash said days so I said we'll create a collection that will use hashing to store the objects so hash set will contain only unique elements and it inherits an abstract set class and implements the set interface so as in when it comes to the hierarchy so what are the API is which it implements or inherits the Reg says it is going to use hashing mechanism to store the element so that is what is hashing so we got a hash function which generates a hash code for the element right so please you can just see to it explore it in case you are not aware of what is a she so we'll create a hash set object as a new hash set and the link so hash set will have the similar methods will have add remove contains size iterator will do lot of stuff in the hash set again we'll see a practical implementation of course guys for the hash second now in the link I said right so this is again a class right which is going to be a hash table and linked list implementation so it's it's gonna be a link asset so it will again contain unique elements so it's gonna provide all the optional set operations and maintains insertion order so Linda said will maintain how the data is getting inserted for us the hash set will not be able to do that she's gonna give us the random or the unordered output so this is how you create an object of late asset and this class inherit methods from the other classes so we got hash set abstract set abstract collection object and set so these are some more of the classes not preset is a sorted version of your set right so whatever the data we're gonna add this will be unique and it will be sorted in the ascending order for us so that is taken care or to matically when you implement a preset so guys when we are talking about preset it inherits abstract set and implements navigable set interface when it comes to the hierarchy so to tree set can be created like this so let us have an introduction to the preset and the hash set so that preset will have the similar methods available right so all those contains remove add this data etc etc so let us come and see one practical implementation on sets so I will write a new class so I'm going to name it as sake demo be the main method so we'll create a hash set so I will maintain the type here as string set is a new hash set so we'll add the data the way we were adding in the list so we'll say John let's say Jenny Jim Jack John and Joe so what we see is John is added three times so it is duplicated thrice and the data which is being added is not indexed right so here data is not added as in indexing approach so we got hash codes for elements which are being added so now here when I try to list the data in the set so as a set is plus set so printing a reference so what you see is that John is added only once not twice so duplicate see is removed that's the first point the second thing is output is unordered now the reason why output is unordered is because of hashing right so for the set there are two observations number one data is unique no redundancy number two observation is where data is unordered in output due to hashing now when you are supposed to iterate in this guy we cannot use new right so there is nothing like indexes here for us that we can iterate but anytime you can use this structure called iterator so you say ITR is set dot iterator we can say while I TR dot has next string name for let's assume yes TR is IP r dot next and you print out the string so hashing technique will not allow us to have indexes so we can iterate using an iterator for Joe John Jack Jenny and Jim data is stored as hash table here so with the hashing so guys we do have other API here so you can anytime come and say said dot remove the element Jim you can say if set dot contains John John is in the set so there are various API is right which you can actually explore so set size is plus and dot size so these are the common methods in all the data structures so after removing the I'll say set after removing Jim is plus the same so let us run this code and see some more epi so it says set after removing Jimi's Joe John Jack and Jenny so you see Jim is removed so John is in the set and the size of the set is who so guys the way we have this hash set which focuses on uniqueness we can create a preset so I can write a tree set so preset shall focus on sorting right so data is unordered but it is alphabetically sorted so the reader will be sorted now if I run the same code here so what I will see is that so we got the data coming as Jack Jenny Jim Joe and John so guys this is alphabetically sorted so what I can do is I can add few nails with different alphabets so let's see I'm gonna see Albert somewhere here I'm gonna see Becca and let's see Cavan so when you run this code here so what you see is you got the data coming as in the sorted arrangement so all these J's they're also sorted now the performance of the tree set is a bit low as compared to hash set because this guy ability an additional time to sort the data as well right so guys preset is a sorted version of hash set so this is what we got for the day so I hope you have enjoyed the session so guys thank you so very much once again this is Shawn on behalf of Enriquez so do like our Facebook page and do subscribe to our YouTube channel thank you guys bye bye have a wonderful time ahead I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to Eddie Rica channel to learn more happy learning
Info
Channel: edureka!
Views: 320,668
Rating: undefined out of 5
Keywords: yt:cc=on, java collection, java collections tutorial, java collection framework, java collection framework tutorial, java collections tutorial for beginners, java collections tutorial edureka, java collections in depth, collections in java, collection framework, collection framework hierarchy diagram in java, collection hierarchy in java, arraylist in java, lists in java, sets in java, interface in java, queue in java, java sets, java edureka, edureka, java training
Id: Ma7u6KEKzPE
Channel Id: undefined
Length: 36min 36sec (2196 seconds)
Published: Mon Nov 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.