Set

3 minutes
Share the link to this page
Copied
  Completed
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behaviour of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements.

Transcript

Hello, in this Java programming video, we are going to look at the set collection and select collection is sort of similar to lists. But the main difference is economic can contain one, you know, type of data or means Sorry, I mean, can it can't contain duplicate elements. And yeah, let's just demonstrate that. So, import java dot utils w utils should say the Asterix There we go. And now we are going to put keywords that angle bracket, now the data type that our set is going to contain in terms of the values to go into our integer. And now I'm just going to call this I should call it call it numbers.

You Okay, so this is the format for creating a set. And now what we're going to do is just add some data to it. For number. Add, let's just add a three numbers in total zero and I love it. Seven, so totally duplicate here. Let's see what happens.

Now that we've got this one printed out so let's do system number. Okay, there we go. It didn't add noise To twice he only added it once one second. So look all those values in no other values are not incorrect. It's just it's not in the order that we added them which is very interesting to say the least. So we got 67 gotten in default 23 or seven and we go Yes.

So got one of values in that. So if we want to actually sort it we can create something called a tree set equals new integer and that was specified yet tool set the muscle and Now, if we print it out, we print out tree for them for system dot n dot print lm. Run it, see what we get. So we get the exact same thing. It is now salted. So that is pretty cool.

There are other methods as well to access those like the first element. The last element, like a whole heap of functions they can use are like numbers dot, you can get the size of the array. You can have a look if it equals something. There's like No, I said a whole heap of stuff. We can do concrete iterator stuff that we've already covered. And I will provide a link to all of the different functionality that you can use with the set collection.

If you have any questions, feel free to reach out and I look forward to seeing you in the next awesome Java video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.