Java Collections Features | Exploring Hidden methods for Developers | @Javatechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java Tei as a Java developers we all use the collections utility in our project which provides a comprehensive set of interface and class to handle collections efficiently however there are few methods that remain hidden from many developers some of you might be aware of them While others might not don't worry in this tutorial we'll explore those lesser known features with simple example okay all right so without any further delay let's get [Music] started so let's begin with our first method that is collections do n copies okay so what this method will does it will return an imitable list containing n copies of specific object for example let's say I want to print or I want to keep Java key 10 times in a list okay for example let me give you a demo collections do n copies If You observe the argument the second argument is object which you want to keep in your list let's say Java Tey how many times you want to keep that in a list so I want to keep it keep it for five times okay so now this particular object will be copied to the list five times now if I'll assign a variable let's say names and if I'll print it let me run it can you see here in the list I'm keeping five copy of this particular object so basically this method will help you while creating the test data for your junit test scenario rather than iterate or uh just Loop the object Loop The Collection you can create this dummy data using nend copies okay and also what we understand this particular list what it generates is imitable it means we can cannot modify it right so let's test that scenario as well so what I want to do let me keep a try and in the same list I want to add something in the zero index I want to add YouTube okay now just catch it unsupported now let me add some statement C out list is now if I run this this part okay let me Zoom this this will return the imitable list but I am trying to modify that particular list let's see whether it is allowing to modify or not let's run it you're getting the error hence it proved whatever the collection. N copies is giving the list that is imitable okay imitable means we cannot modify it fine now let's move to the next method that is let me note it down collections do frequency okay this method itself self explaining the role of it so this particular method is being used to find the frequency of a specific element in a collection for example let's say I have a list this number list okay so in this list I want to find out how many times this particular three occurs or present in my list any element I just want to validate how many times it present in my list so let me add something like 3 6 1 2 6 okay now if You observe here six presents three time in a list and other element so let's try to write a program to find out how many times this particular element present in my list using this frequency method okay this is a simple method now what I can do collections do frequency give the collection from where you want to find out the frequency count for which element you want to find out the occurrence I want to find out the frequency of this particular element in this list it will give you the long as a return type now if I print C out count so I will just modify the statement six presents now let me run this six presents three times okay now if you'll test again let's say four right four is present again three times let me add one more four let me add another four here now in the list four is present five times so if I check and if I'll change the statement and if I run it we can see here four presents five time in a given list so this method is straightforward right and if you remember there is a frequently Asked interview question that find a occurrence of each element in a given string or find the occurrence of each element in given list fine so you can solve such kind of scenario using this frequency for example let me write it find number of or find occurrence find occurrence of each element in a given list I want to perform that using this collections. frequency so that is De easy you can use the uh collectors Group by to solve it within a minute but still you have a option to use this collections. frequency method and you can generate a ma app and you can represent key as a element and value as a number of times it present in that list so to do that what I do simple so just change numbers fine stream then just collect it I want to collect as a map collectors do to map now inside this map what will be my key is nothing my number okay I mean the value from The Collection now let me enter it yeah now what is the value value is nothing the occurrence so how to find the occurrence I can use the collections. frequency number collections do frequency give the list which is number and number that is what the element I want to map so get the key pass it here it will give you the occurrence okay now the third argument we just need to set the existing value in the map and new value to resolve the conflict by just adding the existing value so for that what I can do simple statement so I'll just name something like existing value new value just keep the existing one just add a return statement name it count map or something like that just print now if you see the output four is present five times six present three times two present three times each element is the key and their occurrence in a collection as printed as a value that is what we are doing here right occurrence we are finding we keeping in the value key is the stream I mean we iterating it each element so we are able to get the result fine so if you'll get such kind of question you can use the frequency method or you can use Group by method of collectors which I already explained in my Java programming interview QA fine now let's move to the next method collections do disjoint so what this method will do this method provides a way to check if two collections have any common element if two collection have the common element it will return return true otherwise it will return false with this particular method being a developer we no need to iterate the collection and figure out whether two different collection having the same element or not just create two different collection Or List pass it to this particular method he will give you the answer okay these two ele this these two collection having some common value or not okay so it's very simple to demonstrate that what I can do let's create two different list list one and list two so in list one we have 1 2 3 4 5 here we have 2 3 7 8 9 so if You observe 2 3 is the common element in these two list list one and list two let's see what this disjoint will return to us okay so I'll just use see if You observe the argument it is expecting two collection one is the least one second one is the least two fine now let's add the return value now what I want to do just validate the result if then no common element else there is a common element so just add the C out now just run it can you see here the list of common element now if I remove this 2 three that is what the duplicate here right just remove it you'll see no common element fine so to quickly figure out common between two collection you can use this disjoint method fine now let's move to the next one collections dot single ton okay so what this method does it create an imitable set that contains only a single element for example if I'll use collections do single ton and if I use the object Java takey now it will create a single turn object with the same instance which will be imitable now if I assign the variable see it created the set right so my collection something like that so using this single T collections. single T you can create a single T instance of a single object and again since this is the imitable now if I modify this it should not allow me so to test the I will use the same code which we have defined before my collection so just I just want to add something okay fine now if I run it set is immutable it cannot be modified it means this also created a immutable single tone instance of a collection so this is fine right again these methods will also help you when you want to create a single instance okay so using this collection. single T you can play with it now let's move to the next method collections dot rotate okay this is really interesting method you need to understand what this method does it rotate the element of the specified list by a given distance so alternatively you can say this perform the circular rotation of the element in the list effectively shifting them to left and right based on the number what you will provide for example let's say I'm creating a list and adding 10 element this list will contains 1 to 10 F let me print it for better visibility original list now this list will some something look like this right 1 2 3 4 5 6 now here what I want to do to this particular list I want to rotate the list to the right by three position so I I'll tell you list dot rotate collections do rotate give the list in the particular list I want to rotate the list to the right by three position so first let me give the position from the right take the three position and rotate the list so it means the output will looks like something this so the output will be something like take the three element from the right 8 9 10 then rotate from here it will rotate like this okay you telling to this rotate method take the three element from the right then rotate the list so 8 9 10 then 1 2 3 4 5 6 7 fine so let's run it then we'll understand now if I'll run this see here the original list is 1 to 10 in a sequence then we are telling here to rotate from the right by taking these three element okay start rotating from here take the last three element 8 9 10 then add 1 2 3 4 5 6 this is the right rotation we are doing by specify the index number of a list from where you want to rotate your collection okay now same thing if you want to rotate from the left for example let me comment this I want to rotate from the left by fourth position okay least distance is four so if I give positive number it will consider the right right rotation so I I want to start the rotation from left okay now the rotation is min -4 so what it will do does it will start rotating from 1 2 3 4 okay from here it will start rotating so the output will be five okay let me add it the output will be something like this what we are saying here rotate the collection from left side fourth position okay what is the fourth position this one the output will be something look like this from the fourth position it will start rotating list from the left now let me run this okay I need to print the list right can you see here it took the first four element then it started the rotation that is what the number we have provided here okay so this makes our job easy to rotate the list from a given distance and based on that you can I mean you can basically tell to the collections from what position you want to rotate your list okay that is what the simple statement you can use this rotate method now one small assignment for you to just check whether you understand this roted method or not just let me know in a comment section I'm adding the question here collections. rotate if this list I want to rotate with the list size what will be the output I don't want to run it because this is what the assignment I want you guys to comment with your knowledge what you understand about the roted method so another question if I'll do collections. roted minus 15 very interesting if you understand this method really you will enjoy it try to find out the answer for these two rotation so create a fresh list of 10 or 15 or 20 then try playing with it okay so these two you can give a try so yeah so in this tutorial we explored couple of lesser known features of collections utility like rotate method single turn method disj joint method frequency method and end copies method okay so the Java Collections utility provides a rich set of features there are some more lesser known features which we'll explore in our coming session okay so do let me know in the comment section if you guys have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 11,749
Rating: undefined out of 5
Keywords:
Id: 6i24eXSB5DE
Channel Id: undefined
Length: 20min 0sec (1200 seconds)
Published: Fri May 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.