Podcast Lookup

Java Set & Map Interfaces Explained | Internal Working of HashMap, HashSet & TreeMap

The Java Developers Hub

  • December 3, 2026
  • 1 h 37 min
  • Episode 27
Episode page
About this episodeThe Set and Map interfaces are essential components of the Java Collections Framework. They provide powerful ways to store unique elements…
The Set and Map interfaces are essential components of the Java Collections Framework. They provide powerful ways to store unique elements and manage data using key-value pairs.But understanding how these collections work internally is what separates basic Java knowledge from deeper programming expertise.In this episode, we explore the Set and Map interfaces and take a closer look at the internal working of HashMap, HashSet, and TreeMap. You'll discover how Java stores, retrieves, organises, and searches for data using different collection implementations.From hashing and collision handling to sorting and tree-based data structures, this tutorial will help you understand the mechanisms behind some of Java's most widely used collections.What You'll Learn
  • Understanding the Java Collections Framework.

  • Introduction to the Set and Map interfaces.

  • Differences between Set and Map.

  • Understanding how HashMap works internally.

  • Hashing and hash code generation in Java.

  • Understanding hash collisions and collision resolution.

  • The role of equals() and hashCode() in HashMap.

  • How HashSet works internally using HashMap.

  • Understanding buckets, nodes, and hash table organisation.

  • Exploring the internal structure of TreeMap.

  • Understanding Red-Black Trees and sorted key storage.

  • Differences between HashMap and TreeMap.

  • Comparing HashSet, LinkedHashSet, and TreeSet.

  • Understanding collection performance and time complexity.

  • Choosing the right collection for different applications.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Set & Map Interfaces Explained | Internal Working of HashMap, HashSet & TreeMap

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today's video is going to be very interesting because we are going to study the depth of set interface and map interface. We will see what kind of sets are there, what kind of maps are there, how is their internal architecture.

  2. Speaker 2

    And you must be knowing that set.

  3. Speaker 1

    And maps are the most important data structures used after the list. So you will get to learn a lot. This is also very important for the interview perspective. So let's quickly go to the screen and start.

  4. Speaker 2

    Yes.

  5. Speaker 1

    So in the introduction video about Set and Map, we explained a bit about it. If you haven't seen it yet, then click on the i button, watch that video and come back quickly if you have no idea about Set and Map.

  6. Speaker 2

    If you have a bit of idea, then continue.

  7. Speaker 1

    We know that if I specifically talk about SET, then SET gives us a contract. So SET says that I will tell you two things. The first thing is that duplicates.

  8. Speaker 2

    Are not allowed.

  9. Speaker 1

    And the other thing is that if you store any element in Mer, and if you want to check if that element exists or not, then you can check that in constant time.

  10. Speaker 2

    Constant time search operation. Okay?

  11. Speaker 1

    So these two things are promised by set.

  12. Speaker 2

    Okay, very good.

  13. Speaker 1

    So if we understand this on a basic level, then let's.

  14. Speaker 2

    Say this is our set.

  15. Speaker 1

    We have a little idea how it works from inside, but we will see it again in depth today. So for now, let's assume that this is a black box for us. We don't know how it exists, how it works. We just know that we give it some value to store. Let's say I say to store 2, it stores 2 with us. Then I say to store 3, it stores 3 with us. Let's say I say to store 7, it stores 7 with us. Something like this. And when I ask to store it, I might give some command. For example, I will say, my name is set, so I will say set.add, set.add.

  16. Speaker 2

    Something like this, I will give this command.

  17. Speaker 1

    It says that whenever you ask me to add something, I will quickly add it. After that, whenever you ask me that set.contains, do I have a value named 2, I will give you its answer in constant time. If you have no idea of time complexity, it's okay. You just understand this much. Constant time means, how many number of elements do I have, it doesn't depend on how slow my answer will be. As soon as you ask me, I will tell you whether I have stored 1 element or 1 billion elements.

  18. Speaker 2

    It doesn't matter.

  19. Speaker 1

    How many elements I have. But if you do the same thing in the list. Let's say you have an ArrayList. We know what an ArrayList is. It contains indexes. I will ask it to store 2. It will store it in its first index. Or we call it zero index. Then I will ask it to store 3. It will do it in its first index. Then I will ask it to store 7. It will do it in its second index. Now if I ask this list that do you have 7, then this list will have to perform some action, some operation and what will happen is that this list doesn't know where 7 can be stored, it can be on 0th index, 1st, 2nd or any other index. Right, so what we have to do is, we have to go through each element and ask, if you are 7, he will say no, then I will ask him, if you are 7, he will say no, then I will ask him, and he will say yes. So basically, my answer depends on how long my input size is. If the input size is very long, then I will get the answer a little slow. If the input is very small, then I will get the answer quickly. That's why we say, in the list, its time complexity is order of n, and where n is our input size.

  20. Speaker 2

    The higher the input size, the slower I will get the answer. Right, we know all this.

  21. Speaker 1

    And we also understood what the set promises us. But where will the set be useful? Think of any real-life scenario where the set can be used. Let me tell you. Imagine you are making a website and you just want to know how many users come uniquely on your website. For example, let's say Aditya came on your website, then you registered him. Next time Rohit came, you registered him. But if Aditya comes again, you don't want to register him. You just want to see how many unique users come on my website. Basically, wherever you don't want to deal with duplicates, you can use set, right?

  22. Speaker 2

    Now take another example of this.

  23. Speaker 1

    Let's say I'm telling you to store a list of email IDs. And you know, everyone's email ID is unique, right? No one's can be repeated. So if one email ID, let's say x at the rate gmail.com, it has already come. So if someone is putting it again, then you will not store it again. If x at the rate gmail.com comes once, then you have kept it. Next time when you will store it, then you will store another email id. Let's say y at the rate gmail.com. Right? So it means that any use case where you don't have to deal with duplicates, there we can use our set data structure.

  24. Speaker 2

    Okay, we know this.

  25. Speaker 1

    Okay, we understood this. Now let's talk a little about maps. Maps are also a very interesting data structure. Right?

  26. Speaker 2

    So let's talk a little about maps.

  27. Speaker 1

    Like we said, a set stores some elements and tells us in a constant time if that element exists or not.

  28. Speaker 2

    Maps just does a little extra work.

  29. Speaker 1

    Maps also says that I will not store an element but a key comma value, basically a pair. Where the key will be unique, so if you ask me to store any key value, I will store it. Again, duplicates are not allowed. I will not store the duplicate key, so duplicate key are not allowed. So if you send me a key once, and the next time you send me that key again, I will not store it. And second, again, constant time search. I will tell you in constant time that the value of any key So let's say I want to store the role number comma name of the pair.

  30. Speaker 2

    You know that every student has a unique role number. The name cannot be unique.

  31. Speaker 1

    Many students can have the name Aditya, but the role number will be the same. So let's say the number is 101 and the name is Aditya. I want to store this pair. Let's say the number is 102 and the name is Rohit. And let's say one number is 103 and its name is also Aditya. Something like this. So, the values will not be unique but the key should always be unique. So maps promises you that you can give me any key value set, for example, this is our map and again we consider it as a black box, we don't know how the map works. I just know that I give it some key value and it stores it. So let's say it has stored 101 Aditya in one column in some way, it must have stored 102 Rohit in the same way and it must have stored 103 Aditya in the same way.

  32. Speaker 2

    Now how will it store?

  33. Speaker 1

    It must have some operation, I will say that it is not set, it is a map, so I will keep saying that map.put is a method named put, which I say, take your key 101 and your value Aditya, store it, same I will say map.put 102 Rohit and same I will say map.put 103 Aditya. It will store it to itself. Now it will say whenever you come and query me, and if you ask me, query means you have to ask, that map.contains key, does a particular key exist? Let's say the name of the key is 101. Does this key exist? It will again tell me in constant time that yes, it exists.

  34. Speaker 2

    Yes or no. Basically if it doesn't, it will tell you that it doesn't exist. In constant time.

  35. Speaker 1

    Secondly, if I ask it, map.get, 1, 0, 2. Basically, I'm telling it to give me a value of 1, 0, 2, which is the key. So, it will also give me that in constant time. Right?

  36. Speaker 2

    This promises you a map.

  37. Speaker 1

    Now, whenever I say, those who have the idea of time complexity, whenever I say constant time, remember that it is always average. We say on an average that it will give us in a constant time. This is not compulsory every time. Sometimes it can take the order of n times. If you don't know, we will discuss that later. But on an average, it gives us in a constant time. So I hope you got the idea. Those who had no idea about what is set and what is map. You must have got a little idea of that data structure. Now, look, set and map sound very cool, right? That both of them tell us in constant time that whether any value exists or not, right? Or if we have a map, then it gives us a value of a particular key. But look, there is nothing free in computer science. If you are getting something, then there will be some trade-off, you will also have some loss, right? Now, in the case of set and map, what is the biggest loss we have to make? That is the answer of positional axis. Because now think, If we use a list everywhere, then we just told you what is the disadvantage of a list. When it stores numbers, then it stores on indexes. So 0 is index, 1st index, 2nd index, 3rd index. And if we ask it if it has 5, then it has no other way, rather to ask everyone. But set and map would not store like this. If set and map would also store like this, then they would also need order of end time. So they would have a special way to store numbers.

  38. Speaker 2

    Right?

  39. Speaker 1

    It could be that 5 goes here. It could be that 2 goes here. And why will it go there? There will be a way, a reason, whatever we will discuss. Right?

  40. Speaker 2

    So basically, the value can go anywhere. 3 can go here. 4 can go here.

  41. Speaker 1

    Right? So now when I ask it to print all the numbers of the set, it might print them in a different.

  42. Speaker 2

    Order, like 2, 4, 5, 3. But I put it in another order.

  43. Speaker 1

    So basically, the order in which I put the value, I don't need to get the values in that order. I don't have positional access. That's why I can't search index-based on my set and map. Now before understanding the internal architecture of our own set and map, let's go to the screen and declare a set and map. So I have created an empty file by doing demo.java. So first of all, let's declare a set and then a map.

  44. Speaker 2

    So we know to.

  45. Speaker 1

    Declare a set, because it takes a generic, I will write set of integer, let's say set, small set equal to new.

  46. Speaker 2

    Now how many types of sets do I have? We will see that later.

  47. Speaker 1

    But generally I have three types of sets. I will talk about it. One is hash set, one is linked hash set and one is tree set. We will see all three. But for now I will declare one hash set. So capital will come because it is class. Hash set of integer. Wait, I don't need to write this. I can leave it empty.

  48. Speaker 2

    Hash set.

  49. Speaker 1

    Now let's do one thing, let's import util.star so that all the classes are imported and I don't get any error.

  50. Speaker 2

    Now let's add some values to this set.

  51. Speaker 1

    So I said I will give some values to the set, set.add. Now I have made it an integer set, so I can give some numbers here. Let's say I said, add 4. Then I said, let's make it a string instead of an integer. We have made a lot of integers. So I tell him to add a name, let's say Aditya.

  52. Speaker 2

    He said yes.

  53. Speaker 1

    Then I told him to add another name, let's say Rohit. I wrote a very wrong spelling mistake in Rohit.

  54. Speaker 2

    Rohit, okay. Then I told him to add Rohan. He said okay.

  55. Speaker 1

    I gave him three elements, he added three elements. Now it says, if you ask me, let's say I asked him, that set.contains, do you have Aditya's name? Is there a person?

  56. Speaker 2

    Then he will give me the answer, true.

  57. Speaker 1

    Right?

  58. Speaker 2

    Because he has it. He gave me the answer, true. Very good.

  59. Speaker 1

    If I say a name that doesn't exist, let's say I said, is there a person with this name?

  60. Speaker 2

    Then his answer should be false.

  61. Speaker 1

    Right?

  62. Speaker 2

    It's very simple. So this is called set. Now what can I do in the same way? I can make a map. Now what is a map?

  63. Speaker 1

    It is a key value. So I have to tell what is my key. Let's say my key is integer.

  64. Speaker 2

    Integer.

  65. Speaker 1

    And my value is string. So I have defined it in generics. Let's keep the map's name map.

  66. Speaker 2

    Equal to new.

  67. Speaker 1

    Now what kind of maps do I have? Like I have set. Hash set hash. Linked hash set. And my tree set. Same is with map. I have hash map. Linked hash map. And tree map.

  68. Speaker 2

    So now I am simply declaring my hash map. Hash map.

  69. Speaker 1

    And I declare this. Now how do I add value to the map? I say map.put So I tell it to do one thing, take 101 as the row number and take the name as aditya, store it with you. Same I say map.put, take a row number 102 and store a name in it, let's say Rohit does this. And I say one more thing, map.put, there was a spelling mistake here. So I'll say it once again. Take another roll number 103. This is unique. But take the name Aditya only. It will keep it. Value can be repeated but the key should be the same. Now I'll say two things again. Do you have a key named map.contains? Is there a key named Aditya? The answer should be true. And next time I'll say it. Map.get Map.get I should get the detail of 102 roll number, I have written the value here Aditya, there should be a key here. So let's say I should write 101. So I am asking if the key of 101 exists with you, the answer should be true. Next time I asked, bring the value of the key named 102. Map.get means the value of 102, whose answer is Rohit. So what should be the answer? True and Rohit. So let's see this once by printing. Okay, true and Rohit, this is false, it's printed from above. Okay, so we declared both things once and checked. Now, to understand our set and map internally, let's make our own set and map. So, let's see how our set and map work from inside. We saw this in our collection framework video. But let's take a quick overview. So, let's say this is my set. It will work from inside by using an array. And every element of this array, the cells that we have, let's call them buckets. So, basically, it will use the array of buckets. Now it has an array, so it will have indexing too. Let's say 0, 1, 2, 3. So I have made it only of 4. And what it will do is, whenever you give it a value, let's say you gave it 0, not 0, let's say you gave it 5. So it will put this 5 on one of these 4 indexes. But which index will it put? And obviously, you know, you have to put it in such a way that when I ask it if it has 5, it will give me a constant time. So it will put it in such a way that it will know.

  70. Speaker 2

    Where I have put 5.

  71. Speaker 1

    Right? So I used what if I put 5 on the 5th index, but there are only 4 indexes here. If I take a bigger array, I would put 5 on the 5th index. If someone says 3, I would put 3 on the 3rd index. If someone says 1, I would put 1 on the 1st index. So I would know that whatever number is coming, it exists on the same index. So my work became easy. If someone asks me 5, I will go to the 5th index. If I say 5 is written, then the answer is true. But the problem here is that how big of an array do I have to take? If someone would have put 1 lakh or 10,000 in the index, then how big of an array would I have to take?

  72. Speaker 2

    This is problematic. I can have any number of.

  73. Speaker 1

    Keys, so I would have to make millions or billions of arrays, which is a very inefficient method. So the second method that we adopted is that let's say the size of my array is n. which I am using from inside. Now whatever element you will add, 5, I will modulo that with n, so I will do 5 modulo n. 5 modulo n means, you know what is modulo operator? It tells you what will be the remainder. So when I will divide 5 with 4, because 4 is size, so the remainder will be 1, so I will put it on the first index, so I put 5 here. Now if someone asks me to put 6, I will do 6 modulo 4 and the answer will be 2. I will put it on the second index, so let's say I put 6 here. Now let's say I got 3, so 3 modulo 4 is 3 only, so I will put it directly on the third index. And let's say I got 4, so 4 modulo 4 is 0, I will put 4 here. So it will do something like this. Now what will be the benefit of this? Now, the benefit of this is that no matter how big the number is, if I am modulating it from 4, then I will have a remainder of 0, 1, 2, or 3. Out of these 4, 1 will come. The number cannot go above 4. So, if a remainder comes out of these 4, then I will put it on an index. Now if someone asks me if 6 exists, I will do the same thing. First I will mod 6 from my n which is 4. The answer will be 2. So I know I have to go to index 2. So I will go to index 2 and see if 6 is written. If it is written, then I will say yes it exists. If there was nothing there, then I would say that our number 6 does not exist. So my work here has become a little simple. But again, now another problem comes.

  74. Speaker 2

    What is that problem? That is the problem of collision. Collision.

  75. Speaker 1

    Right.

  76. Speaker 2

    Now what can happen?

  77. Speaker 1

    The problem can be that let's say if I put 4, then 4 will go to the 0th index. What if I put 8? You know, if I modulo 8 with 4, then the answer will be 0 again. So 8 should also go here. But can I write 8 instead of 4?

  78. Speaker 2

    No.

  79. Speaker 1

    4 deserves to come here and 8 deserves to come here. So I went one step ahead. I said, what if I store 4 too? And store 8 too. I store two numbers here. But how can I store two numbers in the same position? The method is to convert each bucket into a linked list. So this bucket is a linked list. Let's say there is no value in it. It is a linked list which holds two values. One value holds 4 and the other holds 8. Something like this. This means that I can imagine all the buckets as a linked list. So this is also a linked list. This is also a linked list, this is also a linked list, everything is a linked list because everyone can hold multiple values. Now why only linked list, why not array? Now I can understand the answer in such a way that there is a problem in array list that I would have stored 4, 8 in array but if someone asked me to remove then I have to do a lot of movements in array. You know if there is an array, there is 4, 8, 5 and I say remove 8, then you have to remove 5.

  80. Speaker 2

    If you have to move it here, then it will be 4-5 like this.

  81. Speaker 1

    There were only two elements behind it, there was only one element. If there were many elements, then you would have to shift them one by one. So I don't want to do all that. That's why I said linkless is simple. Because removing an element here is simple. You just have to remove a node and change the links.

  82. Speaker 2

    That's it.

  83. Speaker 1

    That means our set should look something like this. This is our set. It has different buckets. And each bucket has its own link list. Something like this. It has a link list of the same. It depends on the size of the link list. How many elements I have stored in one bucket. So let's take an example. Let's say I said this is my set. Let's say I said this is my set. Its name is set. And again what is its size? If I talk about its size. So let's say if I see its indexing. So 0, 1, 2, 3, 4. So N is 5. So I have an array of 5 size. Which I call buckets array. Now I am saying that I am not adding numbers. Earlier I used to add numbers. 2 store, 4 store. So I could find index of 2 and 4 using modulo. But now when we saw in our code. So I stored string there. So let's say I told you to store Aditya. So where should Aditya go? Aditya is a string, not a number. But I have a way in Java to convert anything to a number. And what is that way? In our object class, there is a method called hashCode. Since it is in the object class, I can call it on anyone. Because object is the parent of all the class. So I will call hashCode on anyone. After that, I will get an integer value. This is the special thing about hashCode. It gives me an integer value for everything. So if I do hashCode on Aditya, I will get a number. Assume that I have this set. I have a function set.addAditya call like this. What will happen behind the scene? I have made a function add. Let's say it will expect a value. It will expect a value of T type. Because we are talking about generics. So what we will do is we will remove its hash code first. So value.hashcode. We have a number here. I have saved that number in integer. Let's say integer i. Now I will mod that i with n. And we know what n is. Size of our array. Now whatever number I will get after this, I know it will be in the range of 0 to n-1. I will go to that index and store it on one of those numbers. So this will be happening behind the scene. So let's assume in some way, I will remove one quote, we know about this. So let's assume that when I hashed Aditya and then modded it, the answer I got was 0th index So basically my Aditya should go to 0th index. Right now there is no one on 0th index And I know that there can be more elements in the future, so what do I do? I'll make one link here and store my Aditya in it So let's say Aditya came here. Something like this. Now suppose someone said to put Rohit in the next list So let's assume Rohit is here, we get his hash code and then his index. So I get index 1. So let's say I put Rohit here. So Rohit is here. Now you can fill values like this. Let's say the next number is here, it is on my 4th index, so it is here. Now the next name is Rohan, it is also on the 0th index, so I'll put another link in the linkster. And I'll store my Rohan here. Something like this. And this will keep running. Now I'll have to add any element. If it comes for the first time and our bucket is empty, then I'll make a new node.

  84. Speaker 2

    And if it's not empty, then I'll traverse it.

  85. Speaker 1

    Till the end. And at the end, I'll add a new node. Which we used to do in the link list.

  86. Speaker 2

    Right?

  87. Speaker 1

    Now all my work will keep running perfectly. If you understood the set, then the map is also the same. Right?

  88. Speaker 2

    What was there in the map? In the map, we had.

  89. Speaker 1

    Instead of one value, like we were not storing just aditya or 1 We were storing a pair of key comma value Now whenever we talk about a pair, can we store it in a primitive type? Like here we can keep names on strings And if there were numbers here, we could have kept them in integers But here there is a pair, right? Now there is no data type in primitives that can handle a pair So again for this we will need a class, its own user defined data type So let's do one thing, let's make a class, let's say node because generally we name such classes as nodes and again what will be this node? our generics expect this so I will do one thing here I will take k, v let's say I have called the key value as k, v and I will take a k type key and I will take a v type value something like this I have made a node now my work will be just to store this node in that array of buckets what does it mean? absolutely simple this is my map This is my map. Again, there will be a lot of buckets. So, I will make 5 buckets. If we do indexing of this, it will be 0, 1, 2, 3, 4. Something like this. And what I will do is, let's say someone will tell me, first of all, map.put.

  90. Speaker 2

    I told you that this is the function.

  91. Speaker 1

    And let's say we are calling the key as roll number and the value as our name which we saw in the example. So someone will do one thing, do 101 and store Aditya. Now what I had said, what is the unique thing we have? Key is unique, right? So what will happen from inside, I will have made a function of putName. It will expect the key, value from the user. And what will it do from inside? Now obviously if you know that our key is unique and the value is not unique, then we will have to do everything on the key. So if someone says that brother, store the key, value.

  92. Speaker 2

    Then what will I do?

  93. Speaker 1

    First of all, I will remove the hash code of this key. I will remove the hash code as we did earlier. I will store it in an integer i, and mod that i with the size of our array, so I will get an index which will be between 0 and 4. So whatever my index will be, I will store the whole node in it. The whole node means I will store both my key and value. So let's say I got answer 1, or let's say 0 as it was before. So what will I do? I will store the same 1, 0, 1 and Aditya in this. Next time if someone asks how does this work? Let's remove it. Let's say next time someone asks to store 1, 0, 2 and Rohit. And its index is 2. So I will store 1, 0, 2 and Rohit. Now when someone comes to me and says map. Or someone will say contains. That does 1, 0, 1 exist? Or someone will say. get 1, 0, 1. So basically, it's asking if containsKey exists and get wants to get its value. So if someone asks about containsKey, I'll do the same. My get function will look like this. It'll take a key. I'll do the same thing again. Because I only hashed the key, I'll get the hash code of the key. I will get an index, let's say a number, i. I will mod that i with n, I will get an index. And I know that the index will come the same as the one that came the first time to store it. So let's say someone asked, does 101 exist? When I will remove the hash of 101 again, then 0 should come again. And I will go to 101 and see if there is something there. The answer is yes. So I will say true, yes it does exist Now if someone asks me to get 101 So again I will go to 101, whatever its value is, I will give it to the user Like this, right?

  94. Speaker 2

    Because I have stored.

  95. Speaker 1

    The entire node in my bucket Now again you will say if there is a collision, then what will we do? We will use the same principle We will store our link list here apart from storing one number So let's say I stored 101 and Aditya here And here let's say 103, Rohan, I stored it like this. Because it's possible that both 101 and 103 have 0 keys on 0th index. Because modulo operator can give same bucket to many numbers. Which we know that it's removing remainder. So many numbers will have same remainder. You are doing modulo with 5. So 5 modulo 5 will also be 0. 10 modulo 5 will also be 0 etc. So in this way we will make our own map. In which all my work will be done to store the key value. Now if you understand the whole story, then let's go to the internals of Java and see how Java implements the set and the map from within. Now here is a very interesting fact, which many people may not know. There is no data structure called set in Java. Java does not treat the set as a separate data structure. So we were reading the set, we were seeing that the set is different, the map is different. But now think, What is set? What are you being promised by set? Two things that duplicate should not happen.

  96. Speaker 2

    No duplicate. And search operation in constant time.

  97. Speaker 1

    Search operation in constant time. These two things are being promised by set, right? But these two things, these two things will also give you a map. Right?

  98. Speaker 2

    Duplicate is not allowed in the map.

  99. Speaker 1

    Right?

  100. Speaker 2

    It takes constant time to search or to get a key. Right?

  101. Speaker 1

    So, if the principle of both of these is the same. You know that both of them use buckets. And both of them use a link list in that bucket. Something like this. In the case of set, we store a value here. And in the case of map, We store the pair of key, comma, value. In the case of set, whatever value we store, we.

  102. Speaker 2

    Call it unique.

  103. Speaker 1

    And in the case of map, we say key is unique. So can't we say that set is also a kind of map, and we call every value key.

  104. Speaker 2

    Because they are unique.

  105. Speaker 1

    But if every set is a map, then what will be the value of that set? Because we got the key. Whatever the user wants to enter is the key. But what will be the value? Let's see that too. But the most interesting thing is that there is no concept of set in Java. Internally, whenever you create a set, internally java only makes a map the syntax of the set when you write hash set and let's say you add values in it you write set.add from inside all the code gets converted in the map format this is a very interesting thing so basically when you write in your code let's say you declared a set of type let's say integer set equal to new hash set you declared a hash set let's say So from inside, when you go to the code of this hash set, then from inside Java would have changed this code to a map. So basically from inside it would have used a private map. Let's say hash map. And now whose hash map will it use? You had to store integer here, so it will keep key as integer. Now see, there is no value here. So it will put a dummy value of type object. Because object is parent of anything. So it will make a map like this. Map equal to new hash map. Kuch is tarike se. You thought you were making a set, right? But internally, Java made a hash map. Because the same thing we talked about buckets, uses this concept. But it says, what rules should I write for set? The map rules will be set rules. But set was saying, I just have to store keys.

  106. Speaker 2

    No problem. I will put a dummy value in it. Now, value can be anything. Set has no meaning with value.

  107. Speaker 1

    So, no matter what this dummy object is, what difference does it make to us? Set has to do only two operations. Either set has to add, or set has to check.

  108. Speaker 2

    Contains. Right?

  109. Speaker 1

    Apart from this, set can also be removed. Set has to remove a value. But out of all three, set has to perform on a key only.

  110. Speaker 2

    Right? Value has no role there.

  111. Speaker 1

    So what difference does it make if I put a dummy value there? If this is true, then think how all the operations of the set will be converted from inside. It would happen that whenever you write on your set, set.add, first thing we saw that you declared a set from inside, a hash from inside was declared, right? Now whenever you write on your set, set.add to, right? So it will convert this code like this. It will put in the map. You know we add in the set and put in the map. These are the methods. We will see why they are in hierarchy. We put in the map. It will use these two as a key. Because our integer is our key. And it will put a dummy object here.

  112. Speaker 2

    Present. What is present? I will tell you. It will change the code like this. Okay.

  113. Speaker 1

    Now when you write set.contains When you ask if a key named to exists, java will change it to map.containsKey Because it has the method containsKey and it will pass to here and change it like this So whatever operation you are trying to do on set, java is doing it on map because there is no concept of set in java Now an interesting question will arise in your mind that what is present? I had said that it is a dummy object. So Java has made something like this from inside. Java says that I will make a private static final variable. I will make a final variable whose name is present. And again we have kept everything in caps. You know static and final variable means it is a constant. So we always like to keep constant in caps. Present and I declare it as new object.

  114. Speaker 2

    That's it.

  115. Speaker 1

    So you know what happens. Now if you add any key in the set, all the values will be the same, present, which can happen, our values can be duplicated. So imagine if you have this heap memory, then what would happen? Here an object of object type would have been created. And we have a stack memory. In the stack memory there will be a reference variable whose name will be present. which will point it in this way. Now I am saying, whatever you add in the set, add 2, then add 3, add 5, add 10, add anything, all the value will be present.

  116. Speaker 2

    Now what is this?

  117. Speaker 1

    It is a dummy object. It has no work. Its work is just that it does the work of falling down here. Because the set is using the map from inside and you want both key and value. I will have the key, whatever I want to store, I will not have any value. So Java said, do one thing, we will declare an empty object named new object. And everyone will keep on reusing it. What difference does it make? This 2,present is also using it. If I add 4 tomorrow, then 4 will also reuse it.

  118. Speaker 2

    5 too. Because it won't be used anywhere. No one will talk about it.

  119. Speaker 1

    What we will always do in the set is either add, or check the contains, or remove. We will never deal with the value because the value is not in the set. We are just internally creating a dummy object so that we can give that.

  120. Speaker 2

    Thing to the map.

  121. Speaker 1

    So if you understand this, then the rest of the things are very simple. In Java too, whatever we have discussed above, that happens.

  122. Speaker 2

    What does sub mean?

  123. Speaker 1

    Now in Java, you know, set and map, both are stored as a map. Right? So we always store a key comma value in Java. And we store it in exactly the same way. First of all, we remove its hash code. So we find the key's hash code. As we saw above. After that, we remove its bucket. Which bucket should it go in? I call it bucket number How do we get that out? Again we can do a module operation After that we go to that bucket And store that value Now store in bucket Now you know there are two ways Either there is nothing in that bucket So I will directly store it in it And if there is something Then.

  124. Speaker 2

    I will store it in the last element.

  125. Speaker 1

    Of the linked list So that is also the same in this So Java internally does exactly what we just talked about There are a few nuances in it Which we will see now Now the nuances are that we talked about that set and map both are stored inside like a map. And what is a map? It is a node because it has both key and value. So we made it something like this, class node of type let's say k comma v. And what did we store here? We had stored a key of type k and a value of type v. Apart from this, Node stores two more things in Java. One is our hash, So basically whatever hash we take out of this object, that java stores in its container itself. For example, these were our buckets. In this bucket, the node that we will store, not only the key and value will be stored, like we had stored 101 and Aditya earlier. We will not only store this information, we will also store the hash of this key, so that we can compare the hash. Apart from this, we will store a reference of this node, k, v node, which I will call next.

  126. Speaker 2

    But why next?

  127. Speaker 1

    We didn't do this in the case of linked list. Let me tell you why we do this. This is the structure of the entire Java node which Java creates internally. Now, why do we need this next? We understood the key we are storing, let's say 101. We understood the value, let's say Aditya in our case. And we also understood the hash, we stored the hash of this key there. Optimization is the reason for this, because when we compare, we will remove the hash code every time.

  128. Speaker 2

    So we stored it there. But why this next?

  129. Speaker 1

    Next because what I told you, that we have an array of buckets from inside and what happens in every bucket? there is a link list of nodes so if here 101 and aditya are stored then we have to carry this information as well that who is its next node? like suppose 103 rohan is its next node but this link we have to store it as well if there is a link list from inside then we have to store the next pointer in the link list, right?

  130. Speaker 2

    And where do we store it?

  131. Speaker 1

    We have to store it inside the same node class so here also we are talking about the same thing So if this thing is clear to you, then one more thing So where would this node class be? Again, you think, what would be our main class? Our main class would be map, map is an interface, so our main class would be hash map Right? Now the other two types of maps, linked hash map and tree map, we will talk about that in a little while But our main class would be hash map And in that main class, our class would be node So let's say I make it static, so static class node, something like this. And we used to do the same almost work in the linked list. And in this class, our key value hash and our next pointer would come.

  132. Speaker 2

    Okay? So this is our Java's internal structure. In our Java, there is a class called HashMap.

  133. Speaker 1

    And in that class, we have made a static class node which stores all these things.

  134. Speaker 2

    Now let's see.

  135. Speaker 1

    The flow of our main functions. So whenever you make a set, set of let's say string. Let's call it string. S is equal to new hash set. I will put it down like this.

  136. Speaker 2

    And what did I say?

  137. Speaker 1

    When you declare a set from inside, it is using a map from inside. So when you write let's say s.add and you said Aditya. So now let's see what happens inside when you write an add method. When you call this add method So let's say that method is boolean.

  138. Speaker 2

    Why boolean? Because I told you.

  139. Speaker 1

    That the set.add method returns true or false depending upon whether the value was successfully added or not. Which case will not be able to be added? When there will be duplicate value. So let's say I said s.addAditya and the second time it says s.add. If I give the same string both times, then you know that it will not add it the second time. In that case, it returns false. So that's why it is of boolean type. So if you look at it, what would it expect? It would expect a string type key. Something like this. Now obviously, it uses generics, so we.

  140. Speaker 2

    Would have written t here.

  141. Speaker 1

    But for now, I am accepting it because we are working with strings, so it would expect a string type key. So he uses this map from inside. He has already made a map. So basically, he has already made a map in the same class. He will work with things in that map. So from inside, he will say the same map. The map which he has already made in his class.

  142. Speaker 2

    Dot put.

  143. Speaker 1

    Put this key. Take present instead of value. And do you know what present is? He has already declared that in the set class.

  144. Speaker 2

    Present.

  145. Speaker 1

    And this is a new object, means it is an object type object Which it has already declared as static final Store it And here it will write, because it has to return boolean So it writes map.put key comma present equal equal null Now you will understand why it is equal to or equal to null when we talk about the return type of the methods in the maps. But now assume that when we add, it puts from inside. We add key and it puts key comma present from inside. And just check if it is equal to or equal to null. If the answer is true, then this add method will also return true. If the answer is false, then this add method will also return false. Okay, let's assume that for now. So when we called add, put called from inside. Now what will happen inside? So now we have to go inside this put method, the put method of the map. And where will the put method of the map be defined? Inside the map class. So again we will go inside the map class. Inside that will be a put method. We will see about that conceptually here. That how our put method will work. So look, put method will take a key. a value right and it will do some work from inside. Do we know what it will do? First of all, what we talked about above will be exactly the same. It will remove the hash code of this key. So key dot hash code We have done i with mod n and we have got a final index. Which index? We can also call it bucket number where we have to actually store this value. Now what we have to do after that? After that we have to go to that bucket. So now let's go further down. So let's say this is our array. In this we will put some initial size. 1, 2, 3, 4, 5. Let's say 5 also. So 0, 1, 2, 3, 4. So, 5 says that this is our bucket. So, the value of n is 5. Let's write this too. So, n is equal to 5. So, let's assume that the index that came here is 0. So, what we have to do is go inside that index. So, we are standing on the bucket of 0th element.

  146. Speaker 2

    Bucket of 0. If the name of this array is bucket, then I am standing on the 0th element of bucket.

  147. Speaker 1

    Now, as soon as you are standing on this bucket and you have to insert the element, let's say, bucket of 0 and you have to insert aditya comma present right this is our dummy value present we have to store this right this is what we were going to store we had written set dot add aditya right then we converted it key comma present means aditya comma present then we took out its hash code whose hash code we took out key what was the key aditya so here we took out aditya's hash code I got an index I got a value I modded that i from n now I.

  148. Speaker 2

    Got an index.

  149. Speaker 1

    As soon as I get an index, let's say it is 0, I have to enter 0 in it. So for now, I believe that the scenario is that I have to enter bucket 0 with Aditya comma present. Now I have a case here. First of all, case 1 says that my bucket is empty. So basically if my bucket is empty Then I will directly make the first node of my linked list And I will store this information in it So basically I will say like this Bucket of index Which is 0 Is equal to let's.

  150. Speaker 2

    Say new node Which we made node.

  151. Speaker 1

    Class above This one is our node class New node And I will pass value in this First I will pass key So key is our Aditya Then I will pass value Value is our present Then I will pass the hash. So the hash of this will be i. Okay, so I have also stored the hash. So let's say I will pass i. I will write it with a pen. So let's say I will pass i. Let's make it a little smaller. And finally I will store the next link. Now the next link is null. Because I don't have anything else. So I will store this information as a node in my first bucket.

  152. Speaker 2

    Basically in this.

  153. Speaker 1

    Right. This was my case 1. Now what is case 2? Case 2 also you know that my bucket is not empty. Bucket not empty. Not empty.

  154. Speaker 2

    Now what will I do in that case?

  155. Speaker 1

    In that case you know that if my bucket is not empty, then let's say my bucket will look something like this that there will be a link list here, there will be one node, there may be another node.

  156. Speaker 2

    Let's make two nodes.

  157. Speaker 1

    Now I also have two cases inside the bucket not empty. So let's say I make this bucket here again. So I talked about the bucket not empty, there are two cases in it. Case 2.1 says that the one I am going to store which is Aditya present which is my value I is my hash code and I have a reference null. Whatever value I am going to store here, I will keep checking whether that value already exists or not.

  158. Speaker 2

    Because obviously, we will say duplicate values are not allowed.

  159. Speaker 1

    Now what do you think, what can be the reason for coming inside one bucket?

  160. Speaker 2

    There can be two reasons.

  161. Speaker 1

    So any two names, let's say n1 or n2, can be on the same bucket, let's say b of 0.

  162. Speaker 2

    There can be two reasons for this.

  163. Speaker 1

    Either when we did n1 modulo n, then also 0 would have come. And when we did n2 modulo n, then also 0 would have come. One reason could be this. But n1 is not equal to n2. This would be there. That n1 and n2 are not equal. There are two different names. Kaditya and Rohit.

  164. Speaker 2

    They are different names.

  165. Speaker 1

    But when I took out their hash code and moduloed them, then both lied on 0th index. This could be one reason. But the second reason could be that n1 is equal to n2. Maybe I was trying to store Aditya before and now I am trying to store Aditya again. Because we know that if I store Aditya many times, we will get only one hash code. You know that we have talked about the contract of hash code and equals in Java.

  166. Speaker 2

    So we will get only one hash code.

  167. Speaker 1

    So if we get only one hash code, then also it is possible that whenever I add Aditya, it will lie on our 0th index. And I know that I don't have to enter the duplicate code. So if I just traverse it and add a new node, then I will get the duplicates. It is possible that I will add Aditya here as well. Here I will add Aditya in the next node, right? So I will have to keep checking whether Aditya has already been read. So what is case 2.1? Key already present. We will have to check this. Now how will we check case 2.1? So let's say, this is the value that I want to store, right? My current value, right? So something like this will happen in the code. If I make my bucket again, so this was my bucket, this was my bucket 0. And what did I say? In this 0th bucket, a link list is connected, something here, right? So let's say, this will also be a node, this will also be a node, right? So I will do some looping first of all. on this node, then I will first give a temporary pointer, like I travel in the linked list, then I will tell the temp to point the head first, now what will happen to the head, which will point my first node here, then I will say while temp is not equal to null, this is exactly the same code as we use to travel in the linked list, until our temporary pointer is not null, what will we do, we will compare that if our Is the hash of temp not equal to our hash? And we can compare one more thing Is the key of temp not equal to our key? If it is true, then we will say duplicate values Because it already exists Now, as soon as we write duplicate value here, what will happen from inside, whenever we do our map.put and let's say we are using key here, Aditya.

  168. Speaker 2

    Okay, and our present value is there.

  169. Speaker 1

    Whenever we do something like this in the map and our key is duplicate, then the internal structure of our map is such that it updates the value of the old key with our new value. So, we must have an old Aditya node. There will be a value in it. Obviously, that will also be present in this case because it is set. It will update that present with the present. Update it like this and keep the key the same But it will return the value of the old key which was present Obviously in both cases everything is present but it will be different on the actual map It will return present but if it is not a duplicate value i.e. if it would go to else So the map would add that new node finally so basically we would add it to else If I write a code here, then we would do the same thing, we would create a new node, we would put our key in the new node, we would put the value in the value, we would put the present i, null, whatever it is, and we would store something here. So when we do this, then our map returns null. Rest when we see the internal method of the map, you will understand, just remember that when you create a new node, then the map will return null. But when the duplicate value is there, here I should write key instead of value When the duplicate key is there So the map will just put the new value in the old key Whatever we have brought Basically there will be only one key, it won't happen that it will be storing the old key again, no In the same old key, it is updating its new value Now obviously in our case the value is also the same, we are updating the present from the present, that's a different thing But basically the same node will remain, a new node will not be created And it will return the value of the old key, which was present Now think if it is duplicate and it returns present, then what did we write in the starting? In our set method, we said that map calls from inside and map.put key, presents and it checks if it is equal to null or not. And we discussed that when map.put returns null. The null returns when it can add a new value. So when it puts a map. Let's say aditya, present and this never existed, then it returns null. And if null returns, then we will compare null equal null and the answer is true. And this add returns true. Basically we successfully added because there was no duplicate number. Aditya, present went first. But when aditya, present goes to the second time, what will your map.put do? It will change the present from the new value to the old aditya. And what will it return? The old value will return present. And in this case, we will compare if the present is equal to or equal to null. The answer is false. So the return type of our add is false.

  170. Speaker 2

    And when did this happen?

  171. Speaker 1

    When the duplicate value was present. That means, if the duplicate value is there, we will return false. And if the duplicate value is not there, we will return true. Right? Duplicate value means we haven't added a new value. And true means we have added a new value. Right?

  172. Speaker 2

    Okay.

  173. Speaker 1

    I hope you have understood how this whole flow works. If you have understood this, then you will also understand how the contains method of our set works. So let's say I will ask now, that does Aditya exist? So it is exactly the same. This code will use our map from inside. That is, the code of our contains, which will expect a key, will use our map from inside and will check whether the map.contains key This key exists, right? Now how will the map contains key method be made? Right, so you can think that too, it will be the same format. This will be our array, our basically buckets, right? First of all, what will it do? Let's clear its indexing too. Let's say 0, 1, 2, 3. We call them bucket number because indexing will be wrong here. Okay, so again, how will our contains key method work from inside? You can think about this. First of all, we will remove the hash of this key, so we will do dot hash code. We will mod the answer from n to the size of the array We will get an index which we call bucket number We will go to that index and check if our key exists or not Now you know that in this case the index will be 2 And we know that in case of 2 there will be a store for a link So let's make an element here and here.

  174. Speaker 2

    And let's make another element.

  175. Speaker 1

    Let's say there is an element here as well. So again what we have to do is, we have to go through all the elements one by one. And we have to check, what do we have to check? That basically the key that we gave, is it equals? The key written on the node, the node.key, right? It will perform a check like this, on this too, on this too, on this too. If it comes to the last and it doesn't get any element equal, then it will return true. And if it gets any element, then it will return false.

  176. Speaker 2

    Right? It's very simple.

  177. Speaker 1

    We must be able to understand its flow, right? So you can understand the flow of this. Now, this is a very important question. So look, I came back to the same code. When I was adding a key to my set, I was using map.put inside. And what was I trying to put? This aditya, present, this is my key and value. I became my hash and null became my reference. I didn't tell you one more thing that when I add this node to my link list, then obviously in the last node, instead of null, the reference pointer will point to this. So I can change the references of the link list there. That will be happening internally. Think about the second thing. I am comparing two things here. Now the code is a bit wrong here. Here I am checking that first temp.hash is equal to hash.

  178. Speaker 2

    I checked this one.

  179. Speaker 1

    And here it will not come anymore, it will come here and. and and we don't check the key with equal because it only compares references so we will write and temp.key.equals Is it like this? Is the key in this node equal to our key? So why are we checking both these things? And you know that we have used the AND operator, so both should be true. Why are we checking this? Now look, we have also compared the hash. The hash in this node and the hash in our node should be the same. And the key we have and the key you have should be the same. And we checked it using the equals method. And you know, if you overwrite equals, which sets and maps, it doesn't compare references but actual value.

  180. Speaker 2

    Right?

  181. Speaker 1

    So in the case of string, you know there is one habit and another habit. and maybe they are different nodes but dot equals will compare them properly but equal equal operator will not compare them properly we already know that but the main question is that we are comparing hash and value here why are we comparing two things? so let's understand this, we will have an array In that array, we have a lot of buckets, so we call it array of buckets. Now look, what I said is that basically every bucket has a node, basically every bucket will be like a link list, right? Some will have more elements, some will have less elements, there are three on it. Let's say there is only one in it, there is not even one in it, and there are two in it. Let's assume something like this. Let's put their indexing, 0, 1, 2, 3, and 4. Their positions, okay?

  182. Speaker 2

    Okay.

  183. Speaker 1

    Now you think, when you have done map.put, Right, and you must have given key, value here, right? Now, for key, Aditya must have given, for value, you must have given present. If we talk about a set, you must have given anything. If you are using an actual map, and give an actual key, value too. Let's say, you gave 101 and Aditya too. You must have given anything here. They must have finally said this on some index, right?

  184. Speaker 2

    In some bucket.

  185. Speaker 1

    So, I believe, let's say, they will lie all this on bucket 1. Now think, how did we reach bucket 1? So we would have reached like this That whatever we had to store, this 101, Aditya Whatever key part was in it, which was 101 We would have taken out its hash code Right? And we would have modded that hash code with n Right? So basically because of the hash code method We have finalized one bucket in this whole array Right? If its hash code came, we modded it with n So finally we got 1 So the hash code told us That in this whole array in which bucket your element can be found or you can put.

  186. Speaker 2

    It in it.

  187. Speaker 1

    You know that in map.put, if I find an old element, I will update it and if I don't find it, I will add a new element.

  188. Speaker 2

    You know this. So, basically, the hash code just told me in which bucket I have to go.

  189. Speaker 1

    It told me in order of one operation. So, I went directly to that bucket. There are 3 elements in this bucket. Now I have to compare all these elements to see if any of them already exists. If not, I will add the next element. If it exists, I will change it. After this, I will use my equals method. Which we are checking We check that the hash and the hash store in this is the same And the key in this is 101 and the key in this is the same We check all this And in which both the things are the same, the hash and the key are the same Then we believe that this node and this node are the same We have to update in this and not edit the new node.

  190. Speaker 2

    Why is that?

  191. Speaker 1

    Because we talked about a principle In fact, a contract should be called, which was the contract of equals and hash code. Right? We had discussed that what is their contract in Java.

  192. Speaker 2

    So Java says, if two.

  193. Speaker 1

    Keys are the same from equals, that is, let's say one key is K1 and one key is K2. And when I do K1 dot equals K2, and you know, equals does not compare references, it compares exact value. And if its answer is true, then The hash code of K1 should also be equal to the hash code of K2.

  194. Speaker 2

    We know this, right?

  195. Speaker 1

    We have read about this. This is the contract of both of them. If two things are equal, then their hash code should also be the same.

  196. Speaker 2

    But its vice versa is not true. It is possible that their hash code is the same.

  197. Speaker 1

    But it is not necessary that they are equal to each other. One is Aditya, let's say. And one is Rohit. So it is possible that when I took out Aditya's hash code, it got some answer. Let's say the answer is 2023. I am taking any random number. But when I took out Rohit's hash code, it also got the answer 2023. Collision is possible. Now, obviously, 2023 is here and 2023 is here. If you mod these two, then obviously the answer will be the same. So let's assume the answer is 1 for example. So the answer will be the same and the bucket will also be the same.

  198. Speaker 2

    Yes, it could be.

  199. Speaker 1

    Possible that the hash of both will be different and the bucket will be the same. Because the modulo operator will make the bucket of many things the same. Like the bucket of 5 and 10 will be the same if we are doing mod 5, you already know that. But let's assume that their hash is also the same, then the bucket will be the same.

  200. Speaker 2

    Okay.

  201. Speaker 1

    So when I compare their hash code, you must have seen what I wrote in if. I wrote if the hash of my node, node.hash is equal to my hash. So first, I compare these two. So basically, I compare whether 2023 and 2023 are equal or not. The answer is true. So I'm still not sure whether these two strings are equal or not. But even after these two are the same, I'm not sure whether my two strings are equal or not. Because I said that not only the hash code is not enough to tell that they are equal. The contract is the opposite. If it is equal, then the hash code will be the same. If the hash code is the same, then it is not necessary.

  202. Speaker 2

    That it is equal, right?

  203. Speaker 1

    That's why I put another AND operator here and check whether the key of my node.

  204. Speaker 2

    Is that also equal to my key?

  205. Speaker 1

    Right? So I check both of these things. Because if the first one is not equal, then I will move forward. But if the first one is equal, basically their hash is equal, then to be sure that these two objects are not the same, I compare their values too.

  206. Speaker 2

    Basically, I compare the keys too.

  207. Speaker 1

    So suppose this is my new node, which is going to go to my first index. So I will first compare it with my previous node. I will check if its hash is equal to its hash. If the answer is false, then I don't need to check anything else. I will move to the next node. Then I will check if the hash of this and that is the same. If the answer is false, I will move to the next node. But suppose the hash of this and that is the same. The hash of this is also 2023. The hash of this is also 2023. Now I will have a doubt that maybe they are the same object. To check this, I will check the key of these two. What is the key of this and what is the key of this? If this is also the same, then I can say with 100% surety that these two objects are the same. I don't have to create a new link list, I don't have to create a new node. I have to update in this node. Now again you might be wondering why don't we check equals instead of hash because it's very fast. If you know that dot equals method is slow in comparing values.

  208. Speaker 2

    So if we just check.

  209. Speaker 1

    Integers first that not dot hash equal equal is our hash and that is false then it's better to move to next. We don't need to check equals with everyone. So it is a bit optimized that first we check the hash of the node. But if the hash is equal, then we can't be sure. So we have to check the equals too. I hope you understood. Now you can imagine any flow how it works. I showed you the flow of set add which calls the put flow of the map from inside. Now obviously if you understood the set add then you understood the put flow of the map. Same I told you the flow of the set contains. How the set contains works. which calls the contents key of the map from the inside. So you must have understood both. Right? You can also understand the same as remove. What will it do? It will first find its bucket, traverse in that linked list, and if the node exists, it will compare everyone.

  210. Speaker 2

    It will remove the equals.

  211. Speaker 1

    If not, it will come to the end of the list and say that no node exists, it will return false.

  212. Speaker 2

    Now there is one more thing that the map has get.

  213. Speaker 1

    Map.get, you give it a key, it gives you a value. Now obviously in the case of set there is no get, because in set we just have to check whether there is value or not. What will get do, there is no value after that. So in the case of map, we have a get method. So now you can also think about how it will work from inside, it will do exactly the same thing. You gave it a key, it will remove the hash code of this key. That hash code will be modded by the size of our bucket We will get our bucket number Whichever bucket number we get, there will be a lot of elements in it There will be a complete linked list It will traverse on that list It will see if the key of this name exists while comparing with everyone If it does, it will return its value And if it doesn't exist, it will return the answer as null Something like this Okay, so this story is over Now comes one more thing, we had discussed that how we handle collision. The simple way to handle collision is that we use the link list from inside in the same bucket in which the collision is happening. This method is called chaining. So basically you can imagine what we did in each bucket. We added links like chains. We call this chaining. There is another way to prevent collision which we call open addressing. But Java doesn't use it. Maybe you use some other language, but Java doesn't use it.

  214. Speaker 2

    But still, let me tell you. It's very interesting for the interview perspective.

  215. Speaker 1

    Assume you have these 5 elements in the bucket.

  216. Speaker 2

    So 0, 1, 2, 3, 4.

  217. Speaker 1

    Now you gave a string.

  218. Speaker 2

    Let's say Aditya, to store.

  219. Speaker 1

    Aditya is an example of a set that uses a map from inside. You know that a hash code will be performed on this string, then its module will come out. But what will we finally get? We will get an index. So let's say this Aditya should go to the first index. So we will do something like this. Since it is empty, I have stored Aditya here. But suppose the next name is Rohit. And let's assume that its index comes first.

  220. Speaker 2

    How did it come?

  221. Speaker 1

    It must have taken out the hash code, it must have modded, it must have done anything, and it came first. If we don't want to make a link list, then we can do open addressing. Open addressing means that if you can't store here, then store whatever is its next element there.

  222. Speaker 2

    So I stored Rohit here.

  223. Speaker 1

    So although our index for Rohit came 1, but I stored it on its next index. But in fact, this type of open addressing is specifically called linear probing. Basically, we store it by going one step ahead. Now suppose a new person comes, Rohan, and his index is also 1. So how will we store Rohan? First, we will go to 1 and ask, what place is empty?

  224. Speaker 2

    He will say no. So then we will go one step ahead. We will ask 2, is it empty? He will say no. Then we will go one more step ahead. 3 is empty.

  225. Speaker 1

    So let's do one thing, let's put Rohan here. Something like this. Now you will say that we have used 2nd and 3rd index also So now what will happen is that whatever name has to come on 2nd index Let's say the name is Aman And Aman had to come on 2nd index So 2nd is not empty so he will try to come on 3rd If 3rd is not empty then he will try to come on 4th And we will put Aman on 4th So this is also a way but see how complex it is The problem will be that when someone will ask that I want Rohit's value So we will first go to 1 and ask, we will not get value on 1, so we will think it may be on the other, then we will ask the other, we will get value on the other. So basically all the values may be in our cells, may shift a little here and there. We call this open addressing, Java does not use this by itself. But maybe some other language uses it, but the main thing is that we do not use it here, we only use this link list concept in Java. Now if you understand this, then we move on to the next concept, which is load factor. What is the load factor? Again, you imagine your buckets. So let's say 1, 2, 3, 4, 5. We took a 5 size bucket. 0, 1, 2, 3, 4. Which is our array from inside. And there will be a lot of value stores in it. And you know there will be value stores. In the case of linked list, let's say there are 2 value stores in it. Let's say there is not even 1 in it. There is 1 in it. There is not even 1 in it. And let's say there are 3 in it. 3. Something like this.

  226. Speaker 2

    So, this is the condition of your bucket.

  227. Speaker 1

    So, load factor says that there are two things. One is the number of elements and the other is the capacity. So for example, in this case, what are the number of elements? One element is this store, second one is this, third one is this, fourth one is this, fifth one is this, sixth one is this.

  228. Speaker 2

    So basically, we have 6 elements. We have 6 element stores.

  229. Speaker 1

    But what was the capacity? The capacity is the same as the number of buckets we had. But we only had 5 buckets. 1, 2, 3, 4, 5. Right? So the capacity is 5. Now the rule says that we will find a formula of elements divided by capacity. And if the answer If it will be more than 0.75, then we will assume that our bucket array is very full. Right?

  230. Speaker 2

    0.75 means it is more than 75% full. But if you see it, if you do 6 by 5, then it is more than 100% full.

  231. Speaker 1

    Right?

  232. Speaker 2

    Because the space is 5, but we have stored 6 elements in it. Okay?

  233. Speaker 1

    If you want, you can do one thing. For now, you remove this too. Imagine it. And remove this too, let's say. So, now how many elements do we have? We have 4 elements. 1, 2, 3, 4.

  234. Speaker 2

    Right?

  235. Speaker 1

    And space is 5. So, basically, elements by capacity ratio is 4 to 5. Which is 0.8.

  236. Speaker 2

    Right?

  237. Speaker 1

    So, this is also more than 0.75. So, basically, whenever our array is more than 75%, we will do rehashing.

  238. Speaker 2

    Rehashing. What does rehashing mean?

  239. Speaker 1

    The same thing that we do in dynamic array, we will take an array of big size.

  240. Speaker 2

    How big? Double size.

  241. Speaker 1

    So, it was of 5. So, now we will take an array of 10 size. Something like this. All the old keys of it, we will rehash them in this new array.

  242. Speaker 2

    And why will we do this? So that we can reduce the collisions.

  243. Speaker 1

    You think, if we didn't do anything like this, then it is possible that this linked list will keep increasing. Suppose, after this, one more will be added, after this, one more will be added, then one more will be added, then one more will be added.

  244. Speaker 2

    So, there should be some limit.

  245. Speaker 1

    Similarly, if we add 100 elements, then the constant time complexity that we get in our set or map will be completely stopped. This is an order of any. Because now you are traveling in a link list. So we want that the chain of this list should be of minimum length. So in that case, we made a rule that if our threshold, basically load factor is 0.75, which is more than 75% if the array of our bucket is full then we will do rehash in rehashing we follow two steps step 1 create twice as long as array.

  246. Speaker 2

    So if it was of 5 size, we will make a new array of 10 size. So 1, 2, 3, 4, 5, 6, 7, 8, 9.

  247. Speaker 1

    So something like this. After that, we will rehash all the stored keys. Basically, we'll mod it again with 10 this time. Because this time our capacity is 10. Okay, will it make a difference if we mod it again?

  248. Speaker 2

    Yes, it will.

  249. Speaker 1

    Because most of the keys' bucket will change from here. So, first we had 5 elements here. So, what would be the mod of any number 5? If we mod it from 5, it would be 0. But in this case, what will be the mod of.

  250. Speaker 2

    5 mod 10? It will be 5.

  251. Speaker 1

    So the first thing that was stored at 0, this time that thing will be stored at 5. Right? And there will be a lot of keys whose hash will be changed. So we will pick up all those keys, rehash all of them, and put them in the new bucket that will come. What will be the benefit of this? That all the linked lists will be smaller. Because the pair was only of 5 spaces. So the chances of collision were more. Because you can get a value from 0 to 4. But now we have an.

  252. Speaker 2

    Area of 10 sizes.

  253. Speaker 1

    Now any value can come from 0 to 9. So basically the distribution of our value has become better. The size of our linked list has become less. So internally if I tell you, the initial array that java creates is of 16 size. So the bucket array that we created till now was of 5-10 size. The default is of 16 size. So if you see what is 16? This is our capacity. And how do we get the load factor LF? We get the number of elements divided by capacity. And we say that it should be less than 75% Or we can write it as 0.75k So basically if we see what will be the number of elements? The number of elements will be less than 12k Something like this So as soon as we enter more than 12 numbers in our array Our capacity of 16 size will retain its 75% limit So basically we will make a new big array and rehash all the old values Okay, I hope you understood this thing. If you understood this, then let's talk about a new concept which we call treefication. Okay, what is treefication? It's very simple. Basically, after Java 8, Java thought of doing one more optimization and that is thought that this is our array and here our link list is made.

  254. Speaker 2

    Absolutely right.

  255. Speaker 1

    Java said that if the size of this linked list gets bigger, obviously, and if it stays within 75%, then we don't rehash it, just the size of 1 gets bigger. And it took a number, a threshold, let's say 8. The number inside is threshold 8. If its size gets bigger than 8, then this list is already very long. Then basically we will have to do every comparison 8 times. So Java says that internally, I will convert this linked list into a binary search tree. What kind of binary search tree? in self-balancing binary search tree and its complexity will be reduced from order of n to order of log n. Something like this. We will see about self-balancing binary search tree later. But for now, you understand that it uses self-balancing BST and which one? It uses red-black tree. This is a kind of self-balancing BST. Again, this is a bit of a data structure concept. If you don't know, no problem. You can just assume that after Java 8 Plus, as soon as the size of our linked list is more than 8, it changes that list in the binary search tree. What is the benefit? Our time complexity becomes log n. Now when its size will be less than 8 again, it will be converted to our linked list again. And all this conversion happens from inside, we don't know. This is just a way of optimization of Java. If you must be wondering why we use tree already, why are we using linked list? The answer is very simple. Because tree is a bit complex, the structure of node becomes more complicated. Because now we don't have There are two pointers to store, right? So its overhead is more. So we will prefer a small linked list because it is simple. But if things are getting bigger, our list is getting longer, then we will prefer our red-black tree. Now let's move on to our next type of set and map whose name is linked hash set and linked hash map in the same way.

  256. Speaker 2

    So what is this? It is very simple.

  257. Speaker 1

    If you can understand linked in the name itself, it is a normal hash set and a normal hash map. But we have done some linking inside it. What is the linking?

  258. Speaker 2

    Let me tell you. What happens in a normal hash set and a hash map?

  259. Speaker 1

    When we store elements in buckets, their order can be anything when we try to print the entire bucket. So let's say I had a bucket of 5 sizes. So 0, 1, 2, 3, 4 something like this. I said Aditya can store it, so Aditya can go here. I said Rohit can store it, so Rohit can go here. I said Rohan can store it, so Rohan can also go here. Something like a link list can be made. Right?

  260. Speaker 2

    Anything can happen.

  261. Speaker 1

    Now when I will try to print the whole array through an iterator or a foreach loop, then it can be printed in any order. Suppose when I put the order, then I put it in such a way that first I put Aditya, then Rohit and then Rohan.

  262. Speaker 2

    This was my order of insertion.

  263. Speaker 1

    But when I might get values, then I try to fetch like this that first Aditya, then Rohan, then Rohit. So first I get Aditya, That's fine, but what if I get Rohan the second time and Rohit the third time?

  264. Speaker 2

    It can happen, right?

  265. Speaker 1

    Because iterator can travel bucket by bucket and value by value. First it goes to this bucket, there's nothing, it's empty. Then it travels all the elements of this bucket, then this, then this, then this, then this, then this. Your order of insertion can be very different from our order of traversal. So this was the problem of our normal hash map and hash set. LinkedHashSet and LinkedHashMap resolved it. Now, the way you add value, if you add Aditya, Rohit, Rohan first, then when you get the return, you will get Aditya, Rohit, Rohan in the same way. How will you get it? It's a very simple concept. You will enjoy understanding it. What it does from inside, it also uses a doubly linked list. So basically, as soon as you add an element, so let's say if I make it again, So as soon as you put the first element Aditya, let's assume that it comes to index 1. So I directly store it in its link list. Here I stored Aditya. Something like this. Then suppose you put Rohit and Rohit came to the third index. So I store the third index in its link list as well. So here comes Rohit. I'll lower this a little bit. Something like this. Okay, let's remove this. So you first inserted Aditya, then Rohit. So do you know what happened from the inside? When you inserted Rohit after Aditya, from the inside, we would have another linked list running from Aditya, which would have pointed to Rohit. Right? So basically what we did was we saved our insertion order. Now assume the next element came, Rohan. And I said, assume Rohan also came this way. So we put Rohan on the new node. Now the linked list that is running from inside will travel from Rohit to Rohan. And because it is a doubly linked list, it will be on both the sides. You can go from Rohan to Rohit and Aditya too. And same up, you can go from Aditya to Rohit and.

  266. Speaker 2

    Rohit to Rohan too. Okay?

  267. Speaker 1

    Now what we are doing is, as we are inserting elements, we are also preserving their order. Now let's say tomorrow you bring a new name here, and let's say your abhay comes.

  268. Speaker 2

    On 0 index. So what will happen?

  269. Speaker 1

    Our link list had come to Rohan, this time it will point to Abhay. So now you see how your linked list is running, something like this. Now you know, obviously, the head of this, from where our linked list must have started, must have been pointed by someone. Now whenever we call this set iterate, then our iterator, without printing all the elements of each bucket, will travel on the order of this linked list.

  270. Speaker 2

    So it will start with our head, then Aditya, then Rohit, then Rohan, and then Abhay.

  271. Speaker 1

    Now this same concept of hash set and hash map is the same, because both are using hash map from inside.

  272. Speaker 2

    So here, I haven't stored only Aditya, Rohit, Rohan. It could be the keys. There will be a value with them too.

  273. Speaker 1

    If I was making a set, then everyone's value will be present, which is a dummy value. But if I was making a map, then it could be that I am giving a definite value too. It doesn't matter with that. What matters is that the nodes that I have, they are all normal maps' nodes.

  274. Speaker 2

    Because the set also makes maps from inside.

  275. Speaker 1

    All these are connected to each other using a doubly linked list. So think how beautiful this design is. First, we have an array of buckets. in which there can be multiple elements using a linked list and each node combines its own element which is its insertion order. Right, so see, two linked lists work here. One linked list is inside our bucket so that multiple elements can come inside the bucket and the other linked list is to preserve our insertion order. So you can imagine how the structure of our node class would look like. So if I make a node class of this, So let me tell you, this linked hash set and linked hash map, they internally extend our normal hash set and normal hash map. So you understand, the structure of their node will extend the structure of the normal node. What was there in the normal node? In the normal node, you used to store key, value, we used to store a has and a next pointer and the next pointer was the same bucket reference but now because you need a doubly linked list you will store two more pointers whose name will be before and after and the two extra pointers you are storing before and after will be for this doubly linked list this will be the structure of our node in a linked hash set or linked hash map So if you want to preserve your insertion order, then you will always prefer this. But again, because it has an overhead, because we have to make the structure of our node more complex. And internally also, because we are using a linked list, it becomes complex. So we should use it only when we actually need to preserve the order. And in many cases, we don't have to preserve the order, right? So there will be only few cases where you will really feel that you have to preserve the order. In that case, you can use it. And I have told you what it does from inside. It uses double linked list. So its node structure changes a bit. And linked hash set and linked hash map are different classes. What does linked hash set do? It overrides our normal hash set class. And same linked hash map overrides our normal hash map class. Now in the last, I will show you a diagram of how it extends classes. Now let's go to the final. towards the last map which is tree map and tree set. Now obviously you know that all the sets use map only from inside. Remember that last time also that although I have written link hash set and link hash map separately but link hash set is using link hash map only from inside. Like normal hash set uses hash map from inside.

  276. Speaker 2

    Right?

  277. Speaker 1

    Similarly, link has set uses link-map from inside. And the same will happen here. Tree set will use tree-map from inside. Okay, you can assume this. Now, let's come to its structure. Now, look, the map and set written here is a bit misleading. Because actually, we don't use the hashing that we use in map and set. We use again our self-balancing BST.

  278. Speaker 2

    Self-balancing.

  279. Speaker 1

    Balancing BST which we call red-black tree We use red-black tree from inside to store them Now you know that in tree set we will think that there will be only key in our node And in tree map there will be both key and value But from inside there will always be both key and value We only talk about maps from inside What I mean to say is that we will not do that work that we will have a bucket array Right.

  280. Speaker 2

    101 comma Aditya. Let's say we have to store this.

  281. Speaker 1

    So it won't happen that we will first catch 101, we will remove its.

  282. Speaker 2

    Dot hash code.

  283. Speaker 1

    Right? We will mod it with the size of the bucket, let's say 4, and then we will add an index. This doesn't happen here. Here, a completely different thing happens. What happens here? Here, we implement the binary search tree, which is a kind of tree. Why is the name set and the map given here? Because again, it is working in the same way. We can perform the same operation in the set, that we can add, check the contains, and remove.

  284. Speaker 2

    Something like this.

  285. Speaker 1

    We can do the same thing in the map, we can put, we can check the contains key which we were doing and we can check the get key, we can do all this. So we are doing the same thing but their complexity will change. Earlier our hash set and linked hash set and hash map and linked hash map, their complexity was constant on an average. Here their complexity will decrease to order of log n of all operations on an average. So basically, our add contains the remove, put contains the get. All these methods are working in order of log n. But this is slow, right? Order of log n is a slow complexity from order of 1 to constant time. So why is it needed? We are storing things in slow. Because it unlocks many such methods for us which normal set or normal map can't give us.

  286. Speaker 2

    Which methods?

  287. Speaker 1

    For example, if you want to print all your keys in sorted order for any reason.

  288. Speaker 2

    Right?

  289. Speaker 1

    Or let's say you want to find out which is the largest element among all the elements you have entered so far. Or which is the smallest element among them. Or you want to do a range query. Range query means, let's say you entered 1, 10, 20, 35, 74, 83. You entered some numbers like this. Now you have to ask, bring all the numbers between 20 to 75 and give it to me. If you want to do such a range query, for all these, your tree set and tree map is a very good data structure. Again, because he is using self-balancing BST. It is a concept of DSA from inside. If you don't have any idea, then you can go there and read a little bit. But I hope you have understood a little bit. Rest, we will see what will be happening from inside. Okay, you must be having a doubt that if they don't use hashing from the inside, that is, if they don't use a method called dot hash code, then how will they do it in the case of strings if they have to do range query, largest, smallest, or print sorted keys?

  290. Speaker 2

    This is from.

  291. Speaker 1

    Numbers, everything is easy from numbers. But suppose if I am talking about strings, Aditya, Rohit, Rohan, right? How will I perform all these operations in these? What is the sortedness in these? What is the largest and smallest? What is the range query? It can be absolutely. Its answer is lexicographical order. Lexicographical order, right? We also compare the string. Remember, we read in string data structure.

  292. Speaker 2

    Compare to method.

  293. Speaker 1

    Compare to method. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to String S2. String S1 is compared to St Now let's see how it works from the inside This is the concept of our binary search tree And specifically self balancing binary search tree I'll tell you why self balancing in a while Binary search tree has a simple concept Just remember that smaller value goes to left I'm telling you this internally And higher value goes to right Just remember that If you don't know what BST is, go to right. What does it mean? Let's say I am making my own tree set. Which you know will work like a tree map from inside.

  294. Speaker 2

    And what am I storing?

  295. Speaker 1

    Let's say I am storing some numbers in it. So let's say I have to store some keys, let's say 50, 70, 30, something like this. So let's say I took some keys here and I want to store them. Now you can assume that obviously tree set will use treeMap from inside and it will add a value with these keys. So it will make a node class. Now let's see how a node class will look like. It will make a node class. In that, instead of key, it will be key equal to 50. Instead of value, you know, we take a present name dummy object. All that is internal, you know. Now see how we insert in normal BST. 50 is empty, so we will put 50 on the root. Next element is 70. It will go to right. Higher value goes to right. We will store it on the right side of 50. Right and left are obviously conceptual. Everything happens in a heap. 50 is also in a heap. 70 is also in a heap. And these two are connected to each other using a link.

  296. Speaker 2

    What is this link?

  297. Speaker 1

    Basically, the same link that used to be in the list. 50 will have a reference of 70. That is our conceptual design that we put it on the left or right. Now let's see the next element.

  298. Speaker 2

    We have 30.

  299. Speaker 1

    Now we will start the comparison again with 50. Is 30 smaller or bigger than 50?

  300. Speaker 2

    The answer is small.

  301. Speaker 1

    So if the smaller value goes to the left, then 30 will go to the left of 50. If there is no value in the left, then we will enter 30 here. So basically our reference will be something like this. 30 will come here. Something like this. Now let's say the next value is 20. So again we will compare with 50. Is 20 smaller or bigger than 50? The answer is small. So we will go to the left. Now we will compare with 30. Is 20 smaller or bigger than 30? The answer is small. So we will go to the left again. There is no element here, so we will put 20 here. So here comes 20. Something like this. And by doing this, we will also put 40. First we will compare with 50, it will say it is small. Then we will compare with 30, it will say it is bigger than me. So 40 will be on the right side of this. Now the rule is that whatever is on the left side of 50 should be smaller than 50 and whatever is on the right side of 50 should be bigger than 50. And this is true for every node. Whatever is on the left side of 30 should be smaller than 30 and whatever is on the right side of 30 should be bigger than 30. Now let's add 60 and 80. First for 60 we will go to 50, it will say that it is bigger than me Then I will go to 70, it will say that it is smaller than me So we will come to its left So we will enter 60 here And in case of 80, we will ask 50, it will say that it is bigger than me 70 will say that it is bigger than me, so we will come to the right And we will enter 80 here And this became our complete BST In this way we will store the data So all these functions 50, 70, 30, 20, 40, 60, 80 were set.add operations. We must have done set.add 50, then 70 and this way our data must have been added. Now when we ask, when we do this test, does set.contains Our 70, how will it check? Again, same procedure. It will first go to 50. We are not making any hash code here. We are just comparing their values. We will go to 50 and ask that I want 70. It is bigger or smaller than you. Obviously, 70 is bigger than 50. So, we will go to its right. Then we will ask 70, is it equal to 70, big or small, the answer is equal So we will return true that yes 70 exists But if you ask about 90 which doesn't exist Then we go to 50, 50 is bigger than me Then we go to 70, 70 is bigger than me Then we go to 80, 80 is bigger than me Then we come to right, we don't get anything, we get null So we say 90 doesn't exist, we return false something like this So this is the concept of binary search tree. This is a DSA concept rather than Java concept. But here we have introduced a little that you understand at least if you have no idea. Rest if you want to understand about BST then you can watch any video. Just remember that our treeSet and treeMap don't use hashing, they use normal compareTo method and compare values. You know how to find big and small in the case of integer. You know how to find big and small in the case of string. So basically if you compare values, you can make normal treeSet and treeMap. Yes, everything will be a bit slow in this, it will be due to the order of login complexity. Checking or removing contains. But we get the benefit of a lot of good things. like we get range query, it becomes easy to find lower and upper, floor and ceiling value, we will talk about all these functions later. But I hope you got an overview of how we store things. Now why do we call it self-balancing? This was normal BST, we haven't balanced it yet. We say this because the problem with normal BST is that it can skew on one side. Okay, what does it mean? Imagine if I want to enter 50, 60, 70, 80, 90. If I get this number, then let's follow the same rule. First of all, 50 will go to the root. Then I have to enter 60. If 60 is greater than 50, then it will come to the right. Now if 70 is greater than 60, then it will also come to the right. If 80 is greater than that, then it will also come to the right. And 90 will come here. See, all of them are on each other's right. Now, this has become a normal linked list.

  302. Speaker 2

    Right?

  303. Speaker 1

    This has become completely linear. Now, its time complexity will also be order of n. If you have time complexity idea. Because still, we have to go to every node. But we wanted this benefit of tree. We wanted this benefit of tree. That on every node, we decide that we have to go to left or right. So that whatever direction we decide, let's say we decide the left direction, we ignore half of the notes on the right. Right? So look, first if someone asks me that what is set.contains, which is set, what is 80 in this?

  304. Speaker 2

    Let's say he asks 80. 80, right?

  305. Speaker 1

    So, first of all, I ask 50, are you older or younger than 80? 50 says I am older than 80. Sorry, I am younger than 80. That means I have to go to the right. Now, as soon as I went to the right, I did not compare these three elements. I said I will not ask them. Because 50 is smaller than that. In the left of 50, there are all the elements which are smaller than 50. And I have to search for 80. So if 80 is bigger than 50, then it should be found on the right of 50.

  306. Speaker 2

    It won't be found in this. So I ignored three elements.

  307. Speaker 1

    So my search domain was on these three elements. Then I asked 70, are you smaller than 80 or bigger?

  308. Speaker 2

    70 said, I am also smaller than 80.

  309. Speaker 1

    So I ignored its left element and I came to the right. So I ignored it too. Now my search element is of only one element. Now I will ask him if he is smaller or bigger than 80 and he will say that he is equal.

  310. Speaker 2

    So I will return true.

  311. Speaker 1

    So how many comparisons do I have to do?

  312. Speaker 2

    This one, then this one, then this one.

  313. Speaker 1

    But suppose if I don't store them in the form of a tree and store them in the form of a normal linked list like this, then I would have to compare them with everything.

  314. Speaker 2

    This too, this too, this too, this too, this too. So I don't get any benefit.

  315. Speaker 1

    In this case, the order of log n If you don't know how to find the complexity of log n, then you can watch a video on it. But in general, the complexity of the normal link list n, which I have to compare with every element, its complexity is only log n. Right? Because, let's assume that I discard half of the elements on every element that I won't see them. So my search space keeps decreasing. That's why I say that my complexity is log n. How much does it decrease? From 1 by 2, I keep halving my search space. See, here my search space was complete. He said bigger than me, I came here, my search space became half. So he said bigger than me, I came here, my search space became half.

  316. Speaker 2

    Right?

  317. Speaker 1

    That's why we call it log n, its formula is to remove completely. Now just assume that if my node starts to be like this, then I will not get the benefit of the tree. That's why we use self-balancing BST here, which is a red-black tree. What does it do? that he never allows such situations to happen. It keeps balancing the tree before such a situation comes. What does it mean? Look, if I tell you a big tree, then what it will do? As soon as you put 50, it will put 50, no problem. Then you said 60, it will put 60 too, no problem. But as soon as you start putting 70, it will see that if I put 70 here, then my tree has become very big, right?

  318. Speaker 2

    It has skewed this side. I balance it.

  319. Speaker 1

    If I write it like this, that 60 should come at the top, 50 should come to the left of it, My rule is still the same, left of 60 is smaller than 60 and right of 60 is bigger than 60.

  320. Speaker 2

    So it balanced it.

  321. Speaker 1

    So you must have understood what balancing means. So basically, make uniformity on every node. So that we can take this decision every time. That every time we can divide our search space in half. Now let's quickly see how the structure of the node looks like. So we have node class, class node, obviously K, V type key. This will be a key, K type key. A V value. Now we don't need to hash it because it doesn't use hashing from inside. But we need to take a lot of pointers. One will take the left pointer of this node. One will take the right pointer of this node. And it will also take the parent's pointer. See, if you do normal DSA, then generally you will only store left and right in the tree. Never store the parent's reference. But Java makes it more optimized. Java says that 50 has a reference of 30. But 30 also has a reference of 50. Same with 70. is also a reference of 50.

  322. Speaker 2

    Right?

  323. Speaker 1

    So, 30 and 70 are the child of 50 and 50 is the parent of.

  324. Speaker 2

    30 and 70.

  325. Speaker 1

    So, the parent will keep the child's reference and both the child will store the parent's reference too. What is the benefit of this? The benefit of this is that the movement of our tree becomes very easy. Now, I can travel from 30 to 50. I can come to 50 at 70. And I can go from 50 to 30 and from 50 to 70. I can traverse in any direction.

  326. Speaker 2

    Right?

  327. Speaker 1

    So I have three pointers, left, right and parent. And I have another thing, boolean color. Now its value, boolean can be true or false, we can also call it red or black. Now this is again an implementation. So basically red black tree is a type of self-balancing BST. Apart from this, we also have an avial tree. These are again DSS specific concepts. But for now, you can assume that this red-black color just determines how it will be balanced.

  328. Speaker 2

    That's it.

  329. Speaker 1

    If you don't know that, no problem. It's not even the main thing. It's important for us that what all we store. So, key value is there. Apart from this, we have these three pointers. Left, right and parent. We haven't stored any hash here because we don't need it. If you understood all this, then I have to tell you one last thing. After that, we conclude that our hash map and linkedHashMap. So again if we are talking about these, then you can agree that we will talk about hashSet and linkedHashSet because they are both the same thing. All these allow null value as key but only one time obviously because you know that key should be unique and allow null value as value infinite time, you can use it for any number of times. So, basically, in all these four, whether you use hashMap, hashSet or linkedHashMap, linkedHashSet, from inside, obviously, you know that you will be using hashMap and linkedHashMap only. In all these, you can keep the key null only once, but you can keep the value null for any number of times, right? Now if we think about it in detail Value is allowed multiple times because it can repeat the value And key is only allowed once because it should be unique But where does it store the null? How do we get the hash code of the null? There is no hash code of the null So what does java do for optimization from inside? That your byte, your bucket array, buckets The 0th index is a special index and it stores the null key in it. This is a very specific thing. The value of the key is null, no problem. Let's say the key is 101, its value is null. This is possible, there is no problem. But because our tree set or tree map and tree set They don't use hashing, they use compare method And if you do compare with null, you get null pointer exception In these null is not accepted Like key, null is not accepted I hope you understood Now if we see their final hierarchy So this is their final hierarchy We know where set comes, it comes in our collection interface And set itself is an interface So if I want I can write it, set is an interface And sets are derived from two different hierarchies. One is hashSet and the other is treeSet. We know treeSet is a completely different type of set. It doesn't use hashing, so it is made separately. And hashSet is our normal set which uses hashing from inside. Now, our linked hash set extends our hash set class. And if you see the hierarchy of map, you know that map comes out of collection framework, right? Its collection has nothing to do with hierarchy. And we also told you why it comes out. Because map deals with key, comma, value. And whatever comes in collections, they deal with only keys or with one number, right? Now, you know that here, apart from set in collection, list will also come here, queue will also come here. We have already talked about list. We will talk about queue in upcoming videos. And the map has the same hierarchy. There is a hash map below the map and a tree map below the map. And the hash map is extended by the linked hash map. But the thing that you need to remember is that the linked hash map uses the linked hash set from inside. The tree set uses the tree map from inside.

  330. Speaker 2

    And the hash set uses the hash map from inside.

  331. Speaker 1

    Right? So, I hope you have understood the hierarchy. You must have learned a lot in the video. It must have been fun. We will meet in the next video, where we will see all the methods of set, map, linked hash set, linked hash map, and all the methods. That's all for this video. We will meet in the next video. Till then, thank you so much.