Hello.
Yes, students, today we're going to learn implementations of set interface and talk about utility
methods from Collection's class on how that works inside when we review hashad source code.
After this lesson, you'll understand how set are different from list will answer what mechanisms is
used to keep only unique elements inside the set.
I'm going to show you straight safe implementation of that interface and we'll have practice today.
And at the end of the lesson, we'll review with your class, with utility methods.
That is collections class.
Let's start, as you remember, said Interface is located in the collection Iraqi and does not introduce
any new methods.
Do not forget that that is not a list and said by default does not guarantee any order of elements and
index access to the elements.
The main feature of that interface and all of its implementations is that it contains only unique elements
that various business cases when you need to use set, for example, you are retrieving data from different
data sources and you know that data might be duplicated, but you don't need any duplication in this
case.
You just add elements to the set that takes control over all duplicates, or in the case you interact
with user and want to avoid any duplication in information that was provided by user.
You you said there are really a lot of cases where you can understand that you need set.
And now imagine that if you would be asked to implement something that keeps only unique elements and
contain it, how would you do that?
Would you use any other containers that you already know something that's already stores?
Only unique elements?
Try to think for a second before I would give my answer on map implementation store set of keys.
And each key is unique.
There is no sense to have duplicated keys.
That's why in the case we would need to implement set interface.
Most likely we would use any map implementation and delegate method invocations to the map.
But from the side perspective, it would look like we interact only with keys and we have regular collection,
but not the map.
Let's review the most popular implementations of that interface.
I would say that one of the most popular set implementations is a hash set based on my experience and
most of the cases when I need that implementation, I use hash set.
It extends abstract set that gives implementation to some basic operations, namely two equals hash
code and remove all methods and implements.
Set interface hash that is based on the hash map hash that aggregates map inside.
And we'll see this today when we investigate the source code and other popular implementation of that
interface.
Israel hash set this one user linked hash map inside.
It extends hash said main feature of link hash said is that it provides predictable iteration order.
The next set implementation is called Resat.
Can you guess what it is based on?
You are right.
We said use a stream mapping site to keep all elements sorted according to natural ordering, what with
the help of comparator that was provided during the construction of the object.
It also extends Absaroka set and implements navigable set interface that in turn extend sort of set
interface.
And I really believe there will be no need to investigate these interfaces from scratch.
Take into account we reviewed in detail, sorted map and navigable map interfaces.
One will look at the source code.
You understand that implementation of set interface is pretty straightforward for you in case you learned
maps.
Well, that's why I always, along with my students, map interfaces.
First, if you need thread safe implementation of set, you have to choose among different options.
Some of them are Kapyong Right array set the said that is based on compound right or at least that keep
on the unique elements in container concurrence.
Keep list set that is based on concurrence.
Keep list map where elements are sorted according to natural ordering or according to the comparator.
In other words, this is thread safe implementation of navigable set.
And there's also one more option is to use utility methods from collections glass that will churn any
map to set.
There's also one more implementation of set interface that is usually mentioned in books that is and
set.
This type exists specifically to store only enum elements.
To be honest, I didn't use it very often, but it worth to match it.
OK, it looks like now you are familiar with the set Iraqi and now you're ready for the demo, and we'll
start our demo today from overview of hash that Scott.
Here you can see the property of Type Hashmat Parametrized by any type four keys and object for values.
Also, you can see here a constant object that is damage value that will be used as a value that will
be associated with all keys in this map.
Take into account and have said we are not bothering about the values in the internal map.
There is a way to save some memory by putting always the same object as a value into the internal map.
Does it make sense?
You can find different variations of constructors here, but also considering the fact that Hashad is
based on the Hashmat, that in turn is based on the hash table.
We can specify initial capacity and load factor in case we want to change performance a bit.
And after that you can see that implementation of all methods just delegated the course to the map and
adopt interface when it is needed.
Here is a size method that just calls map size.
Inside here is is empty masset that just cause map is empty inside and so on.
Let's look at at method here.
You can see that we call Puth Masset from MAP.
We put the element that was passed as an argument to this set into its map and associate it with the
constant dummy object that we saw at the beginning of our view here.
According to collection interface at Mass, it should return true in the case element was inserted and
force in case element was not added to collection for some reasons.
I believe you remember that put method in map returns the last value associated with this key.
And in case this is in Uki, it returns now.
So in the case this statement is true, that means an element was added to set.
Does it make sense how this makes things clearer?
Let's quickly review Sodded set a navigable set interfaces to make sure that these interfaces are pretty
similar to sorted map and navigable map.
Here is the source code of sorted set interface.
You can see pretty familiar to you interface that has methods for retrieving first and last element
from container and creating different variations of a subset.
Now let's open navigable set interface.
If you remember navigable map interface, you already can see that massas name are familiar to you.
The only difference is there are no methods that returns entry type, but the rest is really similar.
Now let's practice a bit to see how that actually works.
I created this demo file to show you how you can interact with anisette implementation.
Here you can see that I declare the variable of type set parametrized by integer.
That means calculator will watch for me that I would add only integers in this set.
OK, let me add a few elements to the set here and print the output of each at Abberation.
After that, I will print all set to console, let me run this program and you can see that when I added
one the first time it was successfully inserted, but when I tried to insert one, the second time,
it wasn't inserted and we got false.
Two and three were added successfully.
And here you can see my set of numbers.
Only three unique numbers are here without any duplication, just one common here.
I believe you understand that uniqueness of elements is determined based on the hash code and equals
matters, because these two methods help us to place element in hash table.
Let me bring you this great new set parametrized by user type in case you missed homework about online
store.
That is our type from the online store application.
I create two objects of default user type that are the same, but the default user class does not override
hash code and equals masses.
That's why, even in case logically these two objects are equal, set will store both of these elements
and here print it all set elements to console and you can see that both users are in the container even
despised.
They seem to be logical the same.
Now, let me show you another example.
I agree to objects that overwrites equals and hash code matter.
If you remember, during one of the lessons we created the user for hash tables, and this is exactly
the object from our lesson about hash tables.
If you remember here, you can see equals and hash code overridden.
In case I would put these two objects in the set, only one will be added.
Here, I change the first name to William and set a different idea to distinguish users without hash
code and equals methods, and we use these methods of freedom here.
When I printed all users to console one by one, you can see that only one of them was added here.
Also, the things that you have already noticed is that you can use Iterator and foreach loop to iterate
over all elements in set take into account.
It is also implements iterable interface and has its iterator.
Now, you know how to use sets in your work, and the last thing that I wanted to share with you today
is collections glass.
Why do we need it?
Sometimes we need utility mass.
It's like sort of a collection.
For example, let me open the source code of this glass and explore it together with you.
It has really a lot of the masses.
I will not provide my comments for all the masses listed here, but I will tell Fumaroles about only
the most popular ones in my opinion.
And I encourage you to use a source code of this class after the lesson and practice and meet with this
class by calling different methods.
Here we can see Masset.
So sometimes instead of calling sort method on the least, we can call source method of collections
class, you can reverse order of elements in your list if you wish.
You can call reverse Masset and pass reference to any object there if you wish.
Here is a shuffle method that you saw we used for demo purposes in one of the lessons and past.
Also, you can see some of Masset to swap elements by specifying position indexes or field method to
feel content with the specific elements.
He is very useful copying method that allows you to copy list methods to find the least.
And the greatest elements in collection are also here and here.
And one of the coolest methods you can take is a list or said unnavigable, said unnavigable map or
anything other to get unverifiable object.
In other words, the methods that can rub your objects with unquantifiable ropa when in you just imagine
that you implement a method for cars or objects in online store and you don't want to return products
of cards and to let anyone else in other parts of the program to add products directly to the list.
Does it make sense?
What you want instead is that in case somebody wants to add product to the cart, they have to use that
product that contains additional business logic.
It can be tax free calculation or discount of card calculation.
And now we can say that we return on modifiable list because of clients of our cars.
Want to investigate what products in it they can look at them.
There is no harm in this.
But in case they would like to modify the cards, they will catch and unsupported separation exception,
the sad creation of wrappers for one modifiable collections.
We also can create wrappers to turn our regular containers to thread safe containers.
You can find a bunch of methods here that can return to synchronized list or map or set or anything
else.
If you wish, let me open another my file to show you a few more examples.
Collections.
Glass also has Masset that can turn any map implementation to set.
For example, I can create that safe set on the basis of concurrent Hashmat just like this.
Take into account en methods are static and collections class.
I write collections class name after that I call method new said from map and parse the concurrent hash
map object where keys are parametrized with the same type as my set.
According to interface, the values should be parametrized by boolean type and assign this object to
my set variable.
When you might want to use this, for example, you're extremely satisfied with the performance of concurrent
Hashmat and you don't want to use generalized wrapper suggested by collections class.
That's why you might want to use this method to create set implementation exactly on the basis of specific
map implementation.
Does it make sense?
And the last but not the least super useful feature of collections.
Glass is empty constants.
To make you understand what the benefit we can get, I prepared example here for you.
Imagine that you have some in a program that is supposed to return to your list of products that are
associated with a specific course, i.e. let's call this method get products by Karzai.
Awesome.
Now we are trying to retrieve products from database by card idea and imagine we got now in response
because there are no products associated with this card.
Remember, as a rule, my return now from any method you increase the risk of facing with no point the
exception in case somebody would interact with the result of this method.
In other parts of the program, what is recommended?
It is recommended to return empty lists instead to avoid Northpoint exception.
OK, let's look at this option now.
Imagine that you have huge online store and this method is invoked very often.
This will lead you to creation of thousands of objects of our least the need to load your memory even
with empty lists.
I don't think so.
But what is the best option?
In my opinion, the best option in this case is to use predefined constants from collections.
Glass Empty List.
Masset will return you parametrized collection based on the empty list constant if you need you.
And also use empty, sad or empty map, for example, hope this information will help you a lot in the
future.
OK, now I believe we can recap the things that we have learned today.
Today, we learned that Iraqi you learned the main interfaces and the most popular implementations of
that interface after that review, the source code of Hashmat.
And so how set implementations use map implementations inside.
We also had the practice with Hash said, and you saw how hash that keeps only unique elements.
At the end of the lesson, we learned a lot of utility methods from Collection's class.
You also learn best practices of using unverifiable and synchronized robbers.
You also know when and why you have to use empty containers.
Thanks a lot for your attention.
Have a great day and see you in the next lesson.