Java Hashset Tutorial - How To Use the Hashset in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to show you exactly how to use the hash set in Java I hope you're having a great day if you're new here my name is Alex I make a new Java tutorial just like this one every single week so if you're new here then please consider subscribing first thing we'll do to start learning about hash set in Java is just go to file new Java projects cause something like hashes hit finish inside of that project we just created right click on source and go to new clash we'll call it something that isn't isn't hash set because that might cause some problems so I'll just call it like hash browns and I hit the first check mark and then hit finished now we're all set up to start learning so the word hash set might sound pretty confusing and it's like hash map ArrayList hash set what are all these different things these are all names that we give to classes that help us store lots of data in one convenient place and it's the same thing for arrays if we have a simple integer array here like this we can store like say three different numbers in here and to access them we can just do a one like that that would get us this number five here so if I just run this this array is a simple way to store different values the hash set is no different it's a single place to store a lot of different values and we can just access it a little differently so to start practicing with this hash sets we have to make one and so to do that you just type hash set H or name it whatever you want equals new hash set or the capital H and a capital S for set there put some opening close parenthesis and finish it off with a semicolon and this just sets it up for us and then we can use our hash set it's called H but now I'm just gonna hover over it cuz it looks like we've got some red underlined here and no program likes right underlined so I'm just gonna click this import hash set Java dot util and what that does is it generates this import statement at the top and this is just telling Java that we want to bring in that hash set code into this program that we just made so now the red outer lines are gone and now we've got some yellow underlines and if we hover over that and see what it's complaining about it's just saying that hash set is a raw type we should say what types of data are going to be in the hash set because it might be confusing for the computer for this example we'll start off with strings and then I'll do another example afterwards so just type string inside of these little alligators here now it's a little easier for the computer to add data into our hash set just like we would add data to a hash map or an array for our hash set we type H dot and then this dot brings up everything that the hash set can do and we want to add some data it looks like there's one called add here so I'm just gonna type and or you could click it and replace whatever value was in here with the string we want to add to it so I'm just gonna say like lemur say we're storing a bunch of strings that are types of monkeys or of the primate genus not sure of the correct terms but we're just gonna add our little monkey like friends here so maybe we have an orangutan right trying as well and maybe after that we have a spider monkey now our hash set H has some string data in it the name hash set is confusing it's still confusing to me so just like don't worry about that too much I'm gonna show you how to use it Kazuma out here a little if we print out H bring out H like this I just got a new computer a new MacBook and the arrow keys are different sizes so I'm not used to that I'm just gonna save it and run it and here we've got our lemur orangutan I butchered the spelling there probably and spider monkey we can add another one if we once it doesn't have to be only three silverback gorillas all right Pete Harambee save it and run it and you'll see that that grill is now added so that's essentially what this hash set is it's just this set of data you give it and adding is not the only thing we can do we can also remove from it so we can say remove the orangutan save it and run it and now you'll see just the three are in there you can also remove everything by doing H dot clear and that will take all the values inside of it and remove them all so now it's empty and anything you do H dot - you can just kind of look and mess with so here we've got H dot sighs I can just print that out now you see it's size four and they're all there that's because I deleted that clear statement from earlier you can also see if it contains a certain value just as before we just type the name of our hash set dot contains and then what do we want to see if it contains we want to see if it contains the spider monkey spider monkey say then run it and true it'll return true if the spider monkey is in there and it will return false if the spider monkey is not in there so we can see if it contains its a boom a fool and since it doesn't it'll have false that's useful for if Taemin's you could say if the hash set contains a certain value then do this but if it doesn't have it then let's do something else I'll do another example right now but first we'll do this last one it's called is empty and you can see if the hash set is empty it will return true if it's empty and false if it's not empty so let's make another hash set together and I'll show you this cool little trick that we can do to iterate through the hash set using the iterator so I remember how to use a hash set the first thing we do well we know that we can just type hash set to start it off hash set I can name it like hashbrowns because that's hilarious and set that equal to a new hash set object that's empty and ready to be used there's no red underlines because we already have this import Java dot util hash set at the top all we did there was we hovered over it and clicked import Java dot util dot hash set and that generated this code up here now we've got the yellow underlines we just got to say what type let's do integers this time so we'll have a hash set full of integers and the reason that we're doing a capital integer instead of like int like that is because we want it to pass the like object integer instead of like the primitive type integer because it won't work that way so now let's start adding to our hash browns hash browns dot add and we'll add our integer here say like 13 add a few more here say add 24 plenty for it I think I think 24 is my favorite number it's one of them it's up there because it's divisible by so many other numbers and five Horsch browns hash browns and like before we can remove and add and clear just like we did before so we could clear it and you get the idea and now they're all gone if we don't clear it then they're all there what I'm gonna show you right now is how we can loop through them using the iterator before I start that there are actually other ways you can do this to get the values out of a hash set you could do hash browns dots to array like that and that'll turn it into an array it prints out this gibberish because it's like where the array is in memory so what we can do is just say an int array H equals hash browns dot two array and then print out say like H of zero and it wants it to be type object instead of integer so if we save and run that's then we get our 5 notice how it's getting 5 and not 13 that's because you can't rely on the position the order of you adding as the order that it comes back it's sort of hashed all over the place it's like a hashed set so that's why it's called hash set do H of one and we'll get 24 you can also get the hash code of a hash set I'll explain what that is in a second hash code save it and run it and you get just this random number that's kind of like the algorithm it uses to disperse all the values you put into it I think I'm like 90% sure let's get onto that iterator I'm talking way too much to start getting values from a hash set we can just type capital iterator call it like it equals hash browns dot iterator there's a red underline because we need to import that and import the iterator Java util 1 and now it's up there just like before this has yellow underlines because it wants to know the type so we'll just put in our alligators this is going to be integer now to use the iterator we put it in a while loop let me say it dot has next what this says so far is that hey I hear you've got this hash browns hash set and one of the things you can do with that hash set is iterate through it it can return this iterator object so let's just create one of those called it let's try to use it you can use it by typing the name of the iterator dot has next and if the set has the next value then it will keep going and what do we want to do well let's just print out the next value okay save it and let's delete that print statement at the end save it and run it now we get each value 5 24 and 13 so I hope that this helped you learn about hash sets in Java and if it did then you can stick around on the channel I've got plenty of other tutorials just like this one might be able to help you out as well got a lot of plans for this channel so if you're new here consider sticking around I'll see you later have a great day [Music] you
Info
Channel: Alex Lee
Views: 52,270
Rating: 4.9116254 out of 5
Keywords: java hashset, hashset, hashset java, hashset java tutorial, hashset in java, how to write java code, java hashset explained, java hashset tutorial, hashset tutorial, learn java, java, alex lee java, alex lee, hashmap, hashtable, hashset data structure, hash set
Id: PeFyhRr42ac
Channel Id: undefined
Length: 12min 20sec (740 seconds)
Published: Thu Jun 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.