Podcast Lookup

Java Set & Map Collections Explained | EnumMap, IdentityHashMap & Advanced Methods

The Java Developers Hub

  • December 3, 2026
  • 1 h 11 min
  • Episode 23
Episode page
About this episodeThe Java Collections Framework provides powerful data structures for storing, organising, and managing information. Among the most…
The Java Collections Framework provides powerful data structures for storing, organising, and managing information. Among the most important are the Set and Map interfaces, which help developers work with unique elements and key-value pairs efficiently.In this episode, we take a deeper look at Java Set and Map collections, exploring their important methods, implementations, and practical applications. You'll also discover specialised implementations such as EnumMap and IdentityHashMap, and understand when they are useful.Whether you're learning Java collections or preparing for advanced software development, this tutorial will help you choose the right data structures for different programming scenarios.What You'll Learn
  • Understanding the Java Collections Framework.

  • What the Set interface is and how it works.

  • Understanding unique elements and duplicate prevention.

  • Important Set methods: add(), remove(), contains(), size(), and clear().

  • Understanding the Map interface and key-value pairs.

  • Important Map methods: put(), get(), remove(), containsKey(), and containsValue().

  • Understanding HashSet, LinkedHashSet, and TreeSet.

  • Exploring HashMap, LinkedHashMap, and TreeMap.

  • Understanding EnumMap and its specialised key handling.

  • Understanding IdentityHashMap and reference-identity comparisons.

  • Differences between equality and identity in Java.

  • Iterating over Sets and Maps.

  • Choosing the right collection implementation for your application.

  • Best practices for working with Java collections.

Who Should Watch?
  • Beginners learning Java Collections.

  • Computer science and software engineering students.

  • Java developers improving their data structure knowledge.

  • Backend developers managing application data.

  • Developers preparing for Java technical interviews.

  • Anyone interested in writing efficient Java applications.

Transcript

From the publisher

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

Java Set & Map Collections Explained | EnumMap, IdentityHashMap & Advanced Methods

00:00—
  1. Speaker 1

    Yes, so let's start.

  2. Speaker 2

    Welcome back Coder Army to our Java series and today we are going to understand the internal functions of sets and maps, what are the methods, what are the constructors and we will learn a very interesting thing that how internally Java makes the HashMap more optimized. We will learn this at the end of the video, you will enjoy it a lot. So let's go to the screen and start. So let's talk about our set interface first and we know how our hierarchy used to look like. We used to have an iterable interface, below that we had a collection interface and below that we had a set interface. This was the hierarchy. And we also saw what kind of sets we had below the set interface. We had a hash set, a linked hash set and a tree set. If I tell you the hierarchy of all three, it looks like this Let's say our hashSet extends directly from the set And our linked hashSet extends directly from the hashSet And we would have said that our treeSet extends directly from here Extends from our set interface. But it doesn't work like this. There are two new interfaces between set interface and tree set. From which our tree set extends. And why is this important? Because our tree set works to implement both the interfaces. Which will come up now. It works to implement both the methods. It overwrites both the methods. So what are those two interfaces? It extends another interface from set. Which we call sorted set. It extends another interface from that. Which we call navigable set. And finally below this comes our class which we call our TreeSet which overrides all of these. Basically, it will define the navigable set method, the sorted set method, and define the set collection iterable method. So, this is our hierarchy. Generally, we don't talk about these two because they are not that important. These are just basic interfaces that provide some methods. Generally, we directly say that these three are interfaces. The hash set implements the set interface and the linked hash set extends the same hash set. Now here again we have two interfaces So you can say that these two are also our interfaces One is our sortedSet and the other is our navigableSet And finally we have our treeSet Now treeSet gets a lot of methods from these two So sometimes it is important to know Which method is from this and which method is from our navigableSet Now see one very interesting thing We know that the set interface has a collection interface and an iterable interface on top of it. And before the set interface, when we saw the about list interface, we knew that the list also has a lot of methods of its own which overrides the rest of the list, like our array list, Our link list is done. It overwrites all those methods which are in our list interface. Now the interesting thing is that this set interface doesn't have its own method which is special. So all those methods are taken from our collection interface and above that from our itravel interface. The set doesn't have its own methods. Why won't the set have its own methods? Because there is no need for them. Almost all the methods that the set works on come in the collection. We know that the add method is in our collection. The remove method is also in the collection. The contains method is also in the collection. Now apart from this, the basic operations like clear, isEmpty, size, all of these come.

  3. Speaker 1

    Under our collection.

  4. Speaker 2

    So the set doesn't have to define its own method. So this is a very interesting thing about our inheritance and polymorphism that we don't have to remember anything else. The methods that we have read in our collection, all of those methods come under our hashSet and linkedHashSet. They don't get any new method from the set interface that they have to overwrite. Okay? Very good. So all the new methods that we have to study are only in the pre-set Because it has a lot of new methods from navigable set and sorted set Like we have to query range, lower element, floor element or higher element etc. But mainly hash set and linked hash set doesn't have any new methods Except collection and iterable interface which we have seen earlier So let's go to code and declare our set So I have made a demo.java empty file And first of all I have to tell you the constructors of which? First we are going to see about hashSet and linkedHashSet And later we will talk about treeSet because it has its own special methods They don't have their own new methods So let's see their first constructor Okay, so let's say I have to declare a set Set of let's say integer and let's say smallSet equal to new hashSet HashSet. So, obviously, I have an empty constructor. Before that, I will import a star so that there is no error. So, first of all, we have an empty constructor which doesn't take anything. So, from inside, it would make a set. And how big would it be? We saw in the previous video that in general, the starting bucket is of 16 sizes.

  5. Speaker 1

    Right?

  6. Speaker 2

    That is by default. And from inside, you know, HashSet is using a HashMap. So, the bucket size, the array size is of 16.

  7. Speaker 1

    And we know that it has load factor.

  8. Speaker 2

    If our bucket size is almost 12, then we do rehashing. You know all this, so let's ignore it. Apart from this, we have one more override. That is with initial capacity. So you can make a set like this. Let's say I copy this. So I wrote set of integer equal to new set. Or something like this, I made set 2 In which I gave an initial capacity of 100 Something like this So it takes our initial capacity also Now interestingly, I have a third way to make a set In which I can also give capacity Plus I can also give the load factor And you know that by default the load factor is 0.75 So if I want, I can give it the initial capacity of 100 And the load factor is 0.8f And I have to give it in float So I gave it the value in float So by default it is 75% and I said 80% This means that until my initial bucket's list is 80% full, I will not rehash my bucket. If you are not able to understand all this, then you can watch the last video where we talked about hash set, linked hash set and its internal architecture. I will attach that video. Now, obviously, we have a fourth way to make every collection. Using another collection, using another collection.

  9. Speaker 1

    So, what is this?

  10. Speaker 2

    This is basically, let's say we copied this And we have created a new set, we call it set 4. And how will we create it? Let's say a new collection. I have taken a list by doing list.off. 1, 2, 3, 4, 5, 6, 7, 8, 9. Let's assume 9 elements. And where will all those elements go?

  11. Speaker 1

    In our set 4.

  12. Speaker 2

    So basically you can create a set with a new collection. You can give initial capacity and load factor. You can give only initial capacity or you can create.

  13. Speaker 1

    An empty set.

  14. Speaker 2

    Now whatever is true for the hash set, it is also true for the linked hash set. If you want, you can replace the linked hash set in the sub. Even then, if you replace everything with the linked hash set, there will be no problem. This is true for both the cases. These four constructors come in both of us. Because you know that linked hash set is not special It extends the hash set itself It extends the hash set class and puts a new feature of linked list in it So we have seen all the constructors of hash set and linked hash set And apart from this I told you that they don't have any special method So they have the same method which comes from our collection and itravel And we have already seen all that Now let's talk about our most interesting tree set, which has methods from SortedSet and NavigableSet. Now these two give us very interesting methods. So let me show you their list. So these are the range of methods that we get in our tree set. These are some of the above methods from our SortedSet interface and these are some of the below methods from our NavigableSet interface. You might find this too much, but it's very simple. And again, you don't have to memorize these, your IDE will help you. You just need to know a basic idea of what we can do in our tree set. And you know what a tree set is? Tree set is nothing but a self-balancing BST. So, the operation we can do in BST, for which our BST exists as a data structure, we will get all those methods. Okay? So, let's go to the code and look at all these methods. In fact, if you understand this method, In fact, if you understand all the methods of tree set, then you will also understand all the methods of tree map because their names are also almost similar. Because you know how tree set is implemented from inside using tree map.

  15. Speaker 1

    So let's go to the screen quickly.

  16. Speaker 2

    So here I have made a file called demo2.

  17. Speaker 1

    Java and here.

  18. Speaker 2

    We are going to talk about tree set. So first of all, let's make our tree set. Let's say I make set of integer. I call it small set equal to new and I declare a tree set here.

  19. Speaker 1

    Reset.

  20. Speaker 2

    Okay. Now again let's import the star so that we don't get any error. Okay. Now how many constructors are there in the tree set?

  21. Speaker 1

    How many ways to make it? One is obviously we will have an empty constructor.

  22. Speaker 2

    Now since it's not using any array of buckets, so there's no point in giving initial capacity, load factor, etc. We have the fourth method to make any set, which was from another collection. So let's say I have given 4 elements of 2, 5, 7, 9. These are the 4 elements that come in a list. I have made my own set. I'll set 2 here so that there's no error. So I have these two methods. For now, I'll comment it. We're going to go with our first method. In which we made an empty set of a tree set type. Let's do one thing, let's add some integers in this empty set. So I told you, let's say I added 80 and you know how to add things in this, like.

  23. Speaker 1

    It is added in BST.

  24. Speaker 2

    Small values will go to the left of the root and bigger values will come to the right of the root. So set.add, let's say I did 23. I am adding some random values myself, so that we get a range. Now let's do a little big range, let's say 90, something like this. And let's add another value of some middle, let's say 50.

  25. Speaker 1

    Okay.

  26. Speaker 2

    So I have added values of 80, 23, 10, 90, 50. Now let's see some methods that we get from our sorted set interface. And you know where this comes from. Sorted set interface extends our set interface directly. This sorted set is extended by our navigable set and that is extended by our tree set. Basically it implements our tree set. So look, it has very simple methods. So let's say I had this set with some integers. I want to know the smallest value in this set. So obviously we can see that 10 is the smallest. And if I want to know that, I have to simply call first on my set.

  27. Speaker 1

    Set.first.

  28. Speaker 2

    Okay, here it is saying The method first is undefined for type set.

  29. Speaker 1

    Okay.

  30. Speaker 2

    Yes, we made a big mistake here. We made an object of tree set in heap memory. But see, reference variable is of set type. So, in this reference variable set, we can call only that method which will be declared in set. Now, the methods we are going to talk about here are those methods which are specific to our sorted set or navigable set.

  31. Speaker 1

    So, we won't get that in set.

  32. Speaker 2

    Right? So, the best way is that we don't make it like this and make it a tree set. TreeSetOfInteger, something like this. Now I can name this as set. Now see, the error went here in the first. So now see, I made a TreeSet here. Now TreeSet is a class here. Now whatever method I call in this, it will all exist here.

  33. Speaker 1

    So I can do this.

  34. Speaker 2

    Apart from this, if I want, I can also make a NavigableSet type object here.

  35. Speaker 1

    See, I won't get any.

  36. Speaker 2

    Error in that either. Because again, where are all these methods coming from? They are coming from SortedSet, which is above that too. And if I want, I can also make an object of sortedSet. I won't get an error in this because all these methods come from sortedSet. But then again, I won't be able to call all the methods that come from navigableSet. So the best thing is that I make an object named treeSet, sorry, make a reference variable named treeSet. Now I won't get any error, okay? So as soon as I did set.first, I'm expecting that I should get 10, right? See, that's my smallest value. And of course, I got 10 too. Now if my set.first which will be set.last. And you know what set.last will do? It will give me the largest value in this set. And let's print it and see which one it is. It should be 90. Now how is it able to do this? Now you know from inside we store this tree set in the form of a BST. Self Balancing Binary Search Tree. And you know that The special thing about it is that its leftmost node is the smallest And in the same BST, the rightmost most node is the largest This is a property of binary search tree because you know that where do you get small elements?

  37. Speaker 1

    In the left of the root.

  38. Speaker 2

    Now in the left of the root, the smallest element will be in the left. So the element in the left will be the smallest. And similarly, the one in the extreme right will be the largest element. So because of all these positioning, we get access to these methods. And again, all the methods of set take the time complexity of order of log n.

  39. Speaker 1

    I have written long n here.

  40. Speaker 2

    Order of log n. Something like this. Almost all of them take the same complexity in any of our tree set methods. Okay. Now see, after this, I have a very interesting method which is called headset. So basically, I can write my set dot headset.

  41. Speaker 1

    Headset. Okay.

  42. Speaker 2

    Now see, what is it expecting? It is expecting an element from me. I can give it any of these elements which I have put in my set. For example, I have given it 90. Okay. 90 is the most important element. So let's say we give it here. Let's give it 80. Now, what does headset do? It gives me all the elements which are strictly less than 80.

  43. Speaker 1

    So, which elements are less than 80? 23, 10 and 50.

  44. Speaker 2

    So, it will give me a list of 3 elements. If I print it, then it will be printed on my screen.

  45. Speaker 1

    See, 10, 23, 50.

  46. Speaker 2

    Now, obviously, it will not give me 80 because 80 is not included. 80 is not inclusive. So, it should be strictly less. Now, its reverse version also exists. Like I said, it is headset. I also have a tail set. So, I can write So I can write, let's say set dot tail set. In which if I give a number, let's say 80, then which elements will it give me? All those elements which are greater than 80 or equal to 80.

  47. Speaker 1

    So 80 is inclusive in this.

  48. Speaker 2

    In this, our from element which is taking this parameter is inclusive.

  49. Speaker 1

    And in the upper one, that element is not inclusive.

  50. Speaker 2

    Right? It used to give strictly lower in headset. So if I run it, then I should get 80 comma 90. Absolutely. So look, head set is that we get all the lower elements strictly, tail set is that we get higher elements with inclusivity, right? Apart from this, obviously, we can get it in a range of elements. We use for that, let's say, its name is subset. So I can write on my set, set.subset And here I have to give from element and to element. So let's say I want all the numbers in which range? Let's say I want numbers from 23 to 80. So I write 23 here and 80 here. Now which one is inclusive and which one is exclusive? See, from element is inclusive and to element is exclusive. So here we can write from element is inclusive and to element is exclusive.

  51. Speaker 1

    So we have mentioned it.

  52. Speaker 2

    If we know that 2 element is exclusive then 80 will not come. But 23 will come.

  53. Speaker 1

    Now who is between 23 and 80? 23, 50 and then 80. So we think that our result should be 23 and 50. So let's run it once. 23 and 50.

  54. Speaker 2

    Right? This is the one above. Okay, let's comment this. So, these are all the methods that come from our sorted set interface.

  55. Speaker 1

    It's so simple. First, last.

  56. Speaker 2

    And we know headset, tailset and subset. Okay, now let's comment all these. And let's move all the methods that we get from navigable set. Navigable set. Which is another interface. Which extends our sorted set. Okay, so let's see all that. Now see this is also very simple. First of all I have a method set.lower which takes an integer value. Let's say I gave it a value of 80. Now which number will it give me? It will give me the largest number which is smaller than 80. What does it mean? It means largest number Smaller than 80. Right? So which number is smaller than 80 but the biggest one?

  57. Speaker 1

    Right?

  58. Speaker 2

    And I think the biggest number is 23. Because see, how many numbers are smaller than 80?

  59. Speaker 1

    23, 10, and 50. And the largest number among these three is 50.

  60. Speaker 2

    So I should get the answer 50. So if I print it and see, then I got 50. Okay? Now all these things are very useful when we need the nearest lower number, nearest higher number from any number. Now here we have another method which is called set.floor and it also takes a number, let's say I gave it 80. Now it will give me greatest element less than or equal to whatever number I have passed here, let's say 80. Now lower will give me only the largest number which is smaller than 80 and this will give me which is smaller than 80 or equal to 80.

  61. Speaker 1

    What does it mean? Let me tell you.

  62. Speaker 2

    If I do set.floor 80, then I should get the output 80. Because 80 is the largest number which is equal to or less than 80. But when I did it here, I got the answer 50. Right? Because it couldn't get 80. Which number will it always get? Smaller than 80.

  63. Speaker 1

    Right? Now it can get smaller than 80. Not equal.

  64. Speaker 2

    Right? That's why I got 50 here. Look, I got 50 above and 80 below. Now what's the benefit of this? The benefit is that if I give the smallest number 10, in both, if I give 10 here, and here also 10, then set.lower will give null because there is no smaller number than 10 but set.floor will give me 10 only. Okay, let's see this once. See, here is null and here is 10. These two methods may seem similar to some but there is a lot of difference between them. One is lower and the other is floor value. Now if there is lower and floor then we have higher and ceiling too. So basically I can write here higher, sorry set.higher And let's say we pass a number, let's say 80. So I passed 80 here. And apart from this, I have another one which we call ceiling.

  65. Speaker 1

    So I can write set.ceiling. ceiling and here also let's.

  66. Speaker 2

    Say 80 now this higher and ceiling are opposite of lower and floor value so basically higher will give such a number which is smallest number greater than 80 and ceiling will give such a number which is smallest number greater than 80 or equal to.

  67. Speaker 1

    80 so I will write this smallest.

  68. Speaker 2

    Number greater than 80 this should be strictly and this will give such a smallest number greater than or equal to 80 Now we will get 80 in ceiling but in higher we will get 90. So let's see whether we get 90 or 80 prints. Now interestingly all the methods that we used to get from sorted set. Basically all these methods. First, last, head set, tail set, sub set. All those methods are in our navigator. All the methods that we used to have from the sorted set Basically all these methods First, last, head set, tail set, subset All those methods exist in our navigable set as well So if I show all those methods that we have studied till now They were just this, lower, floor, higher and ceiling Now apart from this, we will repeat the same method with a little change Like in this, it was first and last Now we have a method called pole first and pole last So I can write, let's say Set.poleFirst And in the same way, I have another method Set.poleLast Now, what's the difference between normal first and last? Normal first and last give you the smallest and biggest number respectively. They just return it. But pole first and pole last will not only return the smallest and biggest number, but they will also pull it out of the set. So basically, once I have printed pole first and pole last, then my smallest number which is 10 and the biggest number is 90, These two will also be removed from my set. The proof for this is this. Let's do one thing. After this, I'll print again. Set dot, let's say, first. Now, which is my first number? If my 10 is removed, then my first number is 23.

  69. Speaker 1

    Let's see.

  70. Speaker 2

    Let's print this too. I'll sout this first. Something like this. So, first, it will give us 10 and then 90. And then, it will give us 23. So, let's print it and comment the rest.

  71. Speaker 1

    So, let's run it. Absolutely. First, we got 10, 90. Then, we got 23.

  72. Speaker 2

    So, this is the difference between pole first and pole last. And simple first and simple last. We have another very interesting method. Which is descending set. So, if I write set dot. descending set descending set so see what we will get our normal set was this one right 80, 23, 10, 90, 50 now if I call descending set on it so see what result I get See, I got my entire set in descending order 90, 80, 50, 20, 30, 10 So this is the benefit of descending set Apart from this, we also have a descending iterator So let's say I can take an iterator of integer IT And I can get that if I write set.descendingIterator Now I can iterate in this in the same way as I do So I can write while it.hasNext And if this is the case, then I can simply print it.next. Something like this. So let's see which elements I have and all these should be printed in descending.

  73. Speaker 1

    So see 90, 80, 50, 20, 30, 10. So I have a descending iterator and a descending set.

  74. Speaker 2

    We can use any of these to print our set's reverse version or descending order version. Now we will repeat these three methods. We will repeat the head set, tail set and sub set. We will repeat these three methods again but with a little change.

  75. Speaker 1

    Now the head set was that.

  76. Speaker 2

    I will take two elements 80 and I will give you all the elements below that. And what was in this? 80 was not inclusive. Our tail set was that I will take from element 80 and I will give you all the larger elements. But in this our 80 was inclusive. Now, I have some other methods too. For example, if I simply write again, set.headset. So this time, I am not going to use this version, but I am going to use the second version.

  77. Speaker 1

    In which I will pass two values.

  78. Speaker 2

    One is my higher value, from which I need all the numbers. And another boolean variable, that whether I want to include 80 or not. If I take the first overwrite of this method, means basically this one, in which I don't take a boolean variable, then by default it goes false that I don't include it. But let's say I give it true here, that I have to include, my inclusive is true. In that case, I will also get 80. Now if I print it and see, then see I got 80 other than 10, 23, 50. Now in the same way, I have set dot, Set.TailSet. Now I have two things, one from element and I can take a boolean. So let's say I put 80 and whether to include or not. So by default I gave false. So I can do this too. And in the same way I have subset. And which version of this? In which I can give boolean variables. So basically I can say what is the from element? Let's say I have 10. Now if from is inclusive or not, I can give that. What is the to element?

  79. Speaker 1

    Let's say 80. And whether to is inclusive or not, I can give that too.

  80. Speaker 2

    So basically I can do more tuning by using these overrides rather than by using my normal functions. Right, my normal functions were these. Let me remove this once. So first, last, headset, tailset, subset. In these three, I don't get freedom. I have to do what the rule says. Headset says It will not be included.

  81. Speaker 1

    I say okay. Tailset says it will be included. I say okay.

  82. Speaker 2

    Subset says it will be 23. But it will not be 18. Right.

  83. Speaker 1

    Lower element is included. And upper element is not included. I agree.

  84. Speaker 2

    But if I want control.

  85. Speaker 1

    Of myself. Then I will. All these.

  86. Speaker 2

    Basically, I can use overrides in which I can take full control over headsets, tailsets, and subsets. I hope you have understood all these methods better. Now let's move on to our maps and their methods. This was the hierarchy of our map. You know that our map comes out of our collection interface. Right, so below our map, there is a HashMap and it extends our linkedHashMap class and in a very different way, we get a TreeMap. Now, above this too, we have two interfaces in the same way which were in the set. One will be our SortedMap, one will be our NavigableMap and TreeMap will override both of their methods. Now, interestingly, apart from all these, we have a lot of maps. We have HashTable, we have Properties, we have EnumMap, we have identity map, we have concurrent map, we have concurrent hash map, we will see all of these one by one, which are most useful and which.

  87. Speaker 1

    Are not useful at all.

  88. Speaker 2

    Apart from these, there is also a weak hash map. We have many types of maps in Java. Normally, we use hash maps, linked hash maps, and tree maps. And some of these have a specific use case. And there are some legacy classes that we don't use at all these days. Now look, because our map comes in a separate hierarchy, the interesting thing is that the map has to define all its methods by itself. The map doesn't get any of its methods from its collection interface, right? So all the methods we have, let's say size, contains, contains key, whatever we can call it, right?

  89. Speaker 1

    IsEmpty, all of these will have to.

  90. Speaker 2

    Be defined by itself because there is no parent class on it that brings in methods. Interestingly, Java kept the names of all the methods in the map almost the same as the ones we use in the collection interface. So that our naming conventions remain similar and we don't have to remember anything separately. So let's go to the code and see all the constructors and methods. So I have taken a file called demo3. java. Let's make a map of integer comma string. Let's say I call it map equal to new hash map. For now, I'm making a hash map. I can make an empty hash map like this. And I'll do one more thing. I'll import my collection. Not just map, but import everything. So that I don't have any error. First of all, let's add some values to this map. For that, I have a method called map.put in which I can add a key, comma, value. So I take a key 101, let's say it's a roll number, and I take a name, Aditya, which is a string. So basically, the map I'm making here, you can understand it like this, that the map is a map of roll number, comma, names. It maps them.

  91. Speaker 1

    It maps every roll number with a name.

  92. Speaker 2

    So I put a map.put 101, Aditya, let's say I put 102, Let's add Rohit here. Let's add another one, let's say 103 Rohan, like this. I have already added some values here. 103 and Rohan here. Now look, I have all those methods which are very simple. If I want to find a size, I have the method map.size. You know what it will do. It will return the size of my map, which is 3 right now. Apart from this, I have the simple method map.isEmpty. which will tell whether my map is empty or not, the answer will be false. These are all basic methods which were also in our collection framework, here also the name is the same and you know how to use them. So I left these methods here. Now there was one method which was in my collection framework and that was contains.

  93. Speaker 1

    To see whether there is any particular element in any collection or not.

  94. Speaker 2

    But you can't just write contains in the map, you have to tell what contains is. So there are two forms of contains in the map. One is map.contains Contains key, it will take the key, let's say 102, and it will only tell whether the key is 102 or not. So it will give me true or false, now its answer should be true. And the second one I have is map.containsValue. ContainsValue, something like this. And here I will take a value, let's say Aditya. So does the value Aditya exist or not? The answer to this should also be true. So these two methods are very simple. But you should know that contains will take almost constant time. But contains value will take almost order of n times. Now you know why contains will take constant time. is used for key purpose. So we can directly go to that bucket. And that too average constant of time. Yes that average constant of.

  95. Speaker 1

    Start. And but our.

  96. Speaker 2

    Start. But our values. We don't have any hashing on it.

  97. Speaker 1

    So to find.

  98. Speaker 2

    Out that Aditya name's value exists or not. It will have to go to every bucket. And every link list of every bucket. Start. It will have to traverse in every bucket. In fact, if there are multiple nodes connected to the linked list in every bucket, then it will have to go to every list and check if the value of the AdityaName is there or not. Now see, which are the most important methods of the map? Get and put. We have already seen put. In which we put the key and value in a map. Now similarly, I should have a method, map.get. So whoever takes the key from me, let's say 101, And it will tell me that map.get 101 will give me the value of 101 So in this case, I should get aditya So if I print it, I will get aditya But suppose if I have a key that does not exist, let's say 105, then I will get the answer null Apart from get, there was put which we saw earlier. Now let's see a special thing about put. So let's say I put here, map.put and add a new key 104. And let's name it, let's say, Abhay. Something like this. Now see, I'm entering a key that I haven't entered yet.

  99. Speaker 1

    101, 2, 3 and 104.

  100. Speaker 2

    So I'm entering a new key. Now map.put returns me a string. So let's see what string will come first in this case. See, in this case, I got a null string. Now, basically, whenever we enter a new key, then the map will always return us a null. But let's say, I put an existing key, 103. Okay, 103 already existed. And I wrote map.put 103 now. So, what will happen now? It will update Rohan in this 103 key and.

  101. Speaker 1

    Put him now. Okay?

  102. Speaker 2

    But in this case, what will it return us? The old value Rohan will return.

  103. Speaker 1

    If I run it now, then see.

  104. Speaker 2

    Start.

  105. Speaker 1

    So if I run it, I got Rohan in return.

  106. Speaker 2

    But now if I do map.get and here I will get 103, then I will get the answer now, not Rohan. Because the value above me has been updated. So we have to remember that map.put returns null if it enters a new value and returns an old value if it updates an old value. Now, obviously, apart from this, we have map.remove which takes a key, let's say 101. Now let's say it took 101 and it will remove that key from our map. Okay, so this is also very simple. And we have another simple method, which is map.putAll. Now this is nothing, this is a bulk version of put. If you want to put a lot of values, key values pairs together, then you can use putAll.

  107. Speaker 1

    For that, we should have another map.

  108. Speaker 2

    Right, let's say I have made another map of same integer comma string. And I call it map2. Here it will come. Let's say equal to new hash map. Hash map.

  109. Speaker 1

    Hash map.

  110. Speaker 2

    If I want, I can add new values to this map2 and pass it here. So all the values I add to map2 will be put in my old map. So this is my bulk operation put all. Apart from this, I have a very basic method map.clear. which will clear the entire map. So you must have understood that these are the same methods that we used in the previous collection framework, except here we say map.put instead of map.add. And because I have put, I have another method here, map.get. Apart from getPut, the rest of the methods are the same, sizeIsEmpty, we have containsKey, containsValue, we also have remove, putAll, and clear. Now let's come to the very important methods of map. Which will be very useful to you many times. First of all we have the method map.keyset Now let's see what is its return type. Its return type is setOfInteger. Why setOfInteger? Because in our map, the keys were of integer type.

  111. Speaker 1

    So what key set does is.

  112. Speaker 2

    All the keys that we had inserted in this map, it will make a set of those keys. So it will put 101, 102, 103 in it. Why will it make a set? Because we know that our keys are unique. So we will get a set of those uniqueness. So if I want, let's say setOfInteger Let's say, I can collect the set.

  113. Speaker 1

    Now if I print.

  114. Speaker 2

    The set, you know that toasting is overriding here, so it will work like this. So I know that I will get all the keys of my map So see 101, 102, 103 I got it in the form of a set, very good Now if I had this kind of method map.keyset So do I have map.valueset too? Not at all, I have map.values Why value set? Because obviously values can be repetitive Values are not unique So that's why we have map.values And its return type is collection So collection returns us collection of string Because our value is of string type Why does it return collection? Because these can be duplicate values and can't return set. So it returns a random collection. Let's say collection. And I can collect it in string type collection C. Something like this. And if I want, I can print my entire collection.

  115. Speaker 1

    So let's run this once. Aditya, Rohit, Rohan. I got all the values. So, I have this method which is very important.

  116. Speaker 2

    Now, if I have keyset and values, is there any method which can give me all the entries in this map? Basically, either I was getting keys or I was getting values. But I want key value pair.

  117. Speaker 1

    101 Aditya, 102 Rohit, 103 Rohan. Is there any method like this? The answer is yes. If I do map.entrieset. Entrieset.

  118. Speaker 2

    So we will get what we were waiting for. And what does it return? It returns a set of type entry. What is this set? I will tell you now. First we will receive this. Let's say set of type entry. And what type of entry? Integer comma string type. Which I can call entry. And I can correct this. First, we write map.entry. There is no error here. Now, what is map.entry? Let me tell you. In our map interface, there is a sub-interface called entry. If I go into it, interface entry. And what does it contain? In our map interface, map.java is open here. If you go to the top, you will see interface map. So, in this interface map, Entry is an interface of key comma value And what does one entry represent? It represents a pair of key values in this map So for example 101 comma Aditya is an entry 102 comma Rohit is an entry 103 comma Rohan is an entry And entry.

  119. Speaker 1

    Also uses generics So I said.

  120. Speaker 2

    That I will get a set of map dot entry You know that if entry comes inside the map then how do we use it? By doing map dot entry Of type integer comma string Because every pair here is of integer comma string So I will get a set of all of them using map.EntrySet Do you know the.

  121. Speaker 1

    Values that I will get from the set?

  122. Speaker 2

    The first value will be 101 comma Aditya The second will be 102 comma Rohit And the third will be 103 comma Rohan So if I want, I can print the entire entry So first let's name it entries, plural And I will print this entries here Let's see what output we get See, 101 equals to aditya, 102 is rohit, 103 is rohan. That means the entry has also overrided our toString. Now, another very interesting method that we have is getOrDefault. So, what if I write system.out.println, let's say, I have So let's say I write 101, 102, 103, all these exist. So you know that if I write map.get102, I will get its value. And if 102 doesn't exist, I will get null. But I have another method, getOrDefault, in which I can say that if 102 doesn't exist, then what default value do I want? Let's say I want default value unknown. In our case, 102 does exist. So I will not get default value, but I will get Rohit. I got Rohit. But if I am asking for 105, it doesn't exist. In this case, I will get unknown instead of null. So this function getOrDefault is very useful. In fact, in production, we use getOrDefault more than get. Because this makes null comparison easy. We have less chances of getting null pointer exception. Because if we don't get 105, we will pass default value unknown.

  123. Speaker 1

    Right?

  124. Speaker 2

    Now if we have a get and default then there should be an alternative to put and the answer is yes. I have a method putIfAbsent. Now let's say.

  125. Speaker 1

    I put 104,abhi. Now what is putIfAbsent?

  126. Speaker 2

    First I have to write map.putIfAbsent. Now what is putIfAbsent? Put if absent means that if 104 already exists, then I will not update this key.

  127. Speaker 1

    Let's say 103. Does 103 already exist?

  128. Speaker 2

    Yes, Rohan exists.

  129. Speaker 1

    So put if absent will only put the value if 103 does not exist.

  130. Speaker 2

    But because it exists, it will not put this new value. Already what was read will remain read. But if 103 does not exist, then it would have made a new entry 103, now the same. Now you think, how is it different from the normal put method? Our normal put method was, You know that it always replaces.

  131. Speaker 1

    Always replaces.

  132. Speaker 2

    Even though if we pass 103 in put, it would put Rohan in 103. But put if absent will not do that.

  133. Speaker 1

    It will keep the same in.

  134. Speaker 2

    103, which is already Rohan. If 103 doesn't exist, then only it will put a new value. I can write here that put if absent does not replace Existing value So it behaves like this So this is also a very useful method Put, if, applicant, get, default These are both very useful methods, production oriented Now look, obviously we have a remove method in the map Which comes from our object class Look, it will take an object, key Let's say I gave key 101 So remove 101. This will come from the object class, no problem. But I also have an override. Which will take both key and value. 101 and a value too. Let's say Aditya. For example. Now what is this remove? It says that I will remove only if these two key and value match. Is there any key value that 101 and Aditya are the same? So it will remove this. But suppose I have passed 101 and some other name.

  135. Speaker 1

    Let's say Abhay.

  136. Speaker 2

    So there is no such mapping with 101. So it will not remove it. So this is also an override. Now apart from this, I have a method map.replace. I have two overrides. One takes key, value. Let's say 101, sonu. So I took a key, value here. And you know what the simple one will do? What he will do is, he will put Sonu in this key 101, if it already exists, then if 101 was already there, in which Aditya was present, he will replace it with a new name Sonu. Now, how is this different from PUT? If I write PUT here, then also it would be replaced by 101. It is so different that no one will ever put a new key if 101 doesn't exist. So if I don't have 101, then replace doesn't do anything here. But if I write PUT here, then my PUT would put a new value of 101 here. So you know that put will always add a new value if it doesn't exist. And if it exists, it will update the old one. But replace only works for updating, never for adding a new value. Now it has one more override, map.replace. And it takes three things, map.replace, and that is key, old value and new value. So let's say I said 101, in old value I gave Aditya and in new value I gave Sonu. Now what is this override? It says that I will put Sonu in 101 only if its old value is Aditya. So it will match both key and value. Now suppose instead of Aditya it would have been written Abhay. So does it replace it? The answer is no, it doesn't replace it at all. Because I am saying 101 is the old value now and replace it with sonu. It will go and see that it will get the value on 101 Aditya. It will say that the value is mismatched, so I will not replace it. So it is a safe replace. It will replace only if the old value is the same and the key is the same. Now see, we just discovered that we have another interface in map which is called map.entry And what is one entry? Basically one key value pair Now in this class, we get some methods For example, first of all, I will collect map.entry in a set map.entry map.entry.

  137. Speaker 1

    Of type integer comma string.

  138. Speaker 2

    And let's say I name it entries equal to map.entryZ Now if I loop through this let's say map for every map.entry I use for each loop map.entry of type integer comma string and let's name it entry in entries So you know what I did here, I set the map.entry and I got an entry which is the set of map.entry and here I am looping through that set. So I took one map.entry entry in this entry. Now see which methods get unlocked here. I get a method, let's say I want a key. So there is a method, on this entry, on this entry, get key. getKey, it will give me a key, basically whatever key is stored here, I have one method, not integer, here I will get a string, value, value equal to entry.getValue, entry.getValue, right, so I have a getKey method, a getValue method, so I can take them in key, value, and if I want, then I can print them one by one, I printed the key first, then let's say I gave a little space here, okay, Let's write comma and I printed the value. So let's see what will be printed. Let's remove this for now. And let's print this. So see, all my entries are printed one by one. Let's give 101 to Rohit and 103 to Rohan. So we get these two methods, entry.get and entry.getValue in map.entry. Now here we have a static method. Let's make a new map. Of type integer comma string again. And I give it the name map2. So I have another way to make this, which I call map.off. Now again, as we had list.off, in the same way map.off is there. That means it will also make an immutable map. And in this, I can keep giving key, value one by one. So let's say I give it like this, integer is 101, value is Aditya. And let's say the next value is ugly, key is 102 and name is Rohit. In this way, I can keep on giving keys and values. So first I will give K1, then V1, then K2, then V2. And if I give something else, then K3 and then V3. Where K is key and V is values. So in this way, I can make a map, map2. And this will be an immutable map. You know what immutable means. In this map2, I can't enter any new key. So let's say I tried to enter 103, Rohan here. So you know that I will get an error. We saw this in the time of the list. So if I run it and show you, I will get an error of immutable collection. So these were some of the methods that are very famous in our map. We will be using these. Almost many of them were the same. Like isEmpty, contains, getPut was something new, size, clear, all of these were there. Apart from this, we had some new methods like keyset, values, entries, and we had remove and replace methods. Once you understand all these methods, then we can move on to our specific classes like hash map, linked hash map, and tree map. Now let's talk about our hash map and tree map first. About linked hash map Now here Everything is going to be the same If you understood Hash set and linked hash set Then hash map and linked hash map Will also be understood Because you know Internally Hash set and linked hash set Hash map and linked hash map Use it Okay So all the ways Of their constructors The same ways Are also there So let's say I can make a map Again Integer Comma String I call it Map Equal.

  139. Speaker 1

    To New hash map New hash map.

  140. Speaker 2

    So see I can make a map which is So see I can make a map which is empty No one takes any value in constructor Now I put a star.

  141. Speaker 1

    Here Okay What was the second method I had?

  142. Speaker 2

    To make a set I have the same method That is with capacity So let's say I make a new map Map2 And here if I want I can give my capacity Let's say 100 Okay So my bucket size will be that much Okay And what was the third method I had? The third method was That I mention bucket size too And mention my load factor too Let's say 0.8f So I can do this as well. And the fourth method was through another collection. I write this as map3 and this as map4. So let's say I take another collection. I have to take map.off and pass a map in it. Let's say 101 Aditya something like this. So I can do this as well. And if I want I can pass another map instead of map.off If I pass map 3, then also it will work. So I have 4 ways to make a map. And the rest, where I have written hash map, I can replace it with linked hash map. If there is no error, then I will remove it. Here also, here also, here also, and here also. I have put linked hash map everywhere. So again, there are 4 ways to make it. Now let's go to our tree map. And let's see what are the methods in it, which we get from navigable map and our sorted map.

  143. Speaker 1

    Yes, so see.

  144. Speaker 2

    Our map interface has hash map and linked hash map. Apart from this, we had another hierarchy. One was sorted map interface, like we had sorted set. Below that would be navigable map interface, like we had navigable set. And finally, our TreeMap is a class. And you know what treeMap will do? It will overwrite some methods from navigableMap and some methods from sortedMap. So now let's see what methods are there in treeMap. And they will look very similar to the methods of treeSet. So if you understood that then treeMap is a total ditto copy. So here I have taken a file named demo5. java and here I have declared a map of type integer, comma, string. So here we are going to talk about the methods that we get in treeSet. So let me quickly make this map treeMap. And I will also do a tree map here. Because we know that all the methods are tree map specific. So we will not find them in the normal map. Right.

  145. Speaker 1

    Okay.

  146. Speaker 2

    Now let's put some values in it. So I will first copy it from here. From demo 3. And I will put these values here. So, I entered these values in my map. Now, all the methods that you remember were in our tree set. All those methods are in our tree map. Now, if we go to demo2. java where we declared our tree set, then you will remember the names of the methods. Set.first, set.last, headset, tailset, subset. Now here we used to pass first and last because all our values were unique, they won't be duplicated. But when we talk about our maps.

  147. Speaker 1

    You know we have a key and a value.

  148. Speaker 2

    And the responsibility of not being duplicated is only of the keys. The value can be duplicated. So whatever the name of our method is, like first, it will be changed to first key. Now it will be changed to last key. So we will get the same rest, we will just have to use first key instead of last key. Now, interestingly, if we talk about an entry, the entry of the map, which I had told you about, there is one key value pair in our map. So, for example, 101, Aditya is an entry. Now, you know that the key and the value come in the entry as well. So, overall, an entry has become unique, right? Because if our key is unique and the key, value comes in the entry, then our entry has also become unique by itself. So, in some methods, we will have an entry override. I will show you how it will come. So, let's say, first of all, I have a method. map dot first So you know which is the smallest key, that will come to me. So let me print it once.

  149. Speaker 1

    So I got 101.

  150. Speaker 2

    Now here instead of first key, I could have done last key. And that would have been my last key, which is 103. Now instead of first key and last key, I can also print first entry and last entry. My first entry 101 Aditya will come. And if I print last entry. then also you know that 103 Rohan will come.

  151. Speaker 1

    Absolutely.

  152. Speaker 2

    So the first key is the last key, first entry is the last entry. Now we have another method here which was our map.headset.

  153. Speaker 1

    Here we have the method headmap.

  154. Speaker 2

    Now if I give a value here, let's say 103, then you know what will happen? All the values below 103 will be found in a map and I will print it. So if I show you, then see, 101 Aditya and 102 Rohit. I got this in a map and 103 wasn't inclusive here. If I replace the head map with a tail map and write 102 instead of 103, then 102 will be inclusive. If I print this, then I get 102 and 103. So now we have a head map and a tail map. What will be the third alternative? I have a subset over here. I have a sub map. Now what can I pass in the sub map? So I will pass one from key, let's say 101 and pass one to key, 103.

  155. Speaker 1

    So I need all the values of 101 and 103.

  156. Speaker 2

    And I know that from is inclusive and to is exclusive.

  157. Speaker 1

    So I will get the details of 101 and 102. Let's see. See, 101 and 102.

  158. Speaker 2

    Okay. So all these methods used to come from my NavigableMap Like NavigableSets used to come from there Now we have another alternative to this We have our SortedMap So there are many methods in this which are similar to the ones we used there Now there we had lower floor higher ceiling Now here also it is the same, just the name will change I just have to do map.lower And I will get two things, one is lower key and lower entry Now you also know that it will do the same work If I give 102, then what will it bring and give me? It will bring me such a greatest value which is lower than 102. So if I want lower value than 102, then I should get 101. So if I print this, then I will get 101. I will remove this now. Now I just printed this key. So if I want, I can pass the entire lower entry instead of the lower key. Now my entire 101, Aditya should be printed. So 101, Aditya should be printed. Similarly, I have all the alternatives. I have floor entry, ceiling entry, and higher entry. So all the alternatives that I had in the set, I have a copy of them in the map. In fact, there are two copies for a key and an entry. Now again, as I had here, pole first, pole last, right? I also have here, if I show you, map.pole, if I write, then see, pole first entry, pole last entry.

  159. Speaker 1

    So I also have this.

  160. Speaker 2

    Now again, there I had descending set and descending iterator. Here I have map.descending, so I will get descending map. So if I want to print my descending map, then see how the map will look. I will see the opposite map which is currently visible. I will print this. So see, 103, 102 and then 101. And apart from this, if I go back to demo2. java, then I can take the boolean values of headsets, tailsets, and subsets from the tunable method in which I can take their boolean values, that which I have to include and which I don't. I have the same here as well. So if I write map. Let's say head map. So here I can take its inclusive value. Same I have in tail map. Inclusive exclusive value. And if I write map.submap. So I will get two overrides. In which one will be simple from or to. And one will have boolean values too. I hope you got it. It's a very simple method. That's why I didn't describe everything here. If you set it up once, then all the maps will be cleared automatically. So now, if you have cleared the three maps, then let's see some other types of maps that we can make in Java, in which the first one is the hash table.

  161. Speaker 1

    Look, what is a hash table?

  162. Speaker 2

    Hashtable is a legacy class that used to exist in java. Even when our collection frameworks didn't exist, we still had a class named Hashtable. And it does the same thing that our today's hashmap does. It stores key value pairs. And this is our thread safe by default. This is exactly the same as we had a vector class in the list which is now a legacy class. In the same way, the hash table is now a legacy class. This is also thread safe, it works in the hash map, etc. And again, like the vector, today we don't use it at all. We prefer not to use it unless and until it is a legacy code. Why? Because it has a huge overhead and it is very slow to use. If you want to work in a thread-safe environment, then we have a new type of map called Concurrent HashMap. So you can work on this map if you want thread safety. Because it's better and safer. It doesn't lock the entire map like.

  163. Speaker 1

    A hash table.

  164. Speaker 2

    So nowadays we use Concurrent HashMap if thread safety is important. Otherwise, hash table was used earlier and not anymore. Apart from this, we had another hierarchy. So from hash table, which is now our legacy class, another class used to extend from it, whose name was properties. Now which class is this and what kind of map does it represent? Now look, this is a kind of specialized map. If I write this, then this is a specialized map. Which represents a configuration data, config data. So if you read Spring Boot or Spring Framework, then you will see that there are many configurations. For example, username equal to admin. Right? Password equal to 123, anything.

  165. Speaker 1

    Equal to 123.

  166. Speaker 2

    So all these things, we call them configurations. Which helps us to run any application. Now these configurations are mostly text-based.

  167. Speaker 1

    Text-based. Now you can see.

  168. Speaker 2

    Here also there is user name text, admin text, password 123 All are text or strings So to represent this type of configuration we use properties Which is a type of map only Now you will get a lot of use of this when we start our spring boot series. So if you don't have any idea, then you can wait for it. But if you know a little bit, then you will understand what I am doing here. In fact, many people use properties, but they never know that it is a part of map in Java. And it extends our legacy class hash table. Now apart from this, you will see another type of map in Java, which we.

  169. Speaker 1

    Call weak hash map.

  170. Speaker 2

    And this is a map that maybe someone uses it. It is very rarely used. It can be a very specific use case. If you want a cache-like behavior, then you can use it to create a cache-like behavior. And this weak hash map comes out because of weak references. And what are these weak references? We will study this when we will study memory management. Then you will understand better what are weak references and how weak hash map works. But look, you should definitely know that there will be very few use cases that you will need weak hash map. If you want a cache-like behavior which you have to implement, then maybe you can use it, but that too is a very rare chance. So you can assume that it is used almost non-existently today. And you will know the reason when we will study about memory management. Because it is going to be used a lot here. Now apart from this, Java has another type of map, which we call identity hash map. So Identity HashMap. Okay, what is this? Obviously, it sounds like a kind of HashMap, right? But how is it different from a normal HashMap? See, the normal HashMap we have, that HashMap is our Equality Based. Equality Based. Okay, what does Equality Based mean? So, equality based means, as we told you that this is the array of buckets and these are the multiple buckets. And you know that every bucket has multiple link lists. Because if there is a collision in one, then multiple values can come. Now whenever we enter a new value in this map So let's say this is the map of integer comma string So we want to enter 101 comma aditya here Where 101 is our key So what we used to do? First we used to remove the hash code of 101 And then we used to modulo it with the size of the bucket So in this case 1, 2, 3, 4, n has a size of 4 So I will remove the hash code of 101 and modulo it with 4 And let's say my value comes as 0 index So 0th bucket comes as my value This means that this value should be inserted in 0. Then we would see that if I want to put 101, Aditya in this 0th index, then all the values that are already there in the linked list, none of them have 101. Because if someone already has 101, then I will not make a new list, a new node, but I will change its name to the same old value. So how did we compare? We used to compare using equals. We used to check if the key here is equals to the key.

  171. Speaker 1

    We are bringing here.

  172. Speaker 2

    So we used to use the equals method which we used to use in our object class. Now the special thing about the equals method is that let's say we have two strings A and B. And in both of these there is a new string Aditya. New String Aditya So you know that it will be created in heap memory And here also it is written New String Aditya And this will be created in a different object in heap memory Even if Aditya is written in both of them But what will happen in our heap memory Two different objects will be created If Aditya is written in both of them inside the string Right?

  173. Speaker 1

    But these will be two different objects. Now in their.

  174. Speaker 2

    Stack memory, there is a reference variable a which represents it and a reference variable b which represents it. But, even if two different objects are made here, if I write a.equals b, then you know that its answer is always true. Why? Because the string has overrided the equals which actually compares their values, not their references. And what is the value of both of them?

  175. Speaker 1

    It's the same. Right?

  176. Speaker 2

    This means, if the string has overrided the equals, Then the hash code will also be overrided. Basically, if I take out the hash code of A, then it will also be equal to the hash code of B. Because their values are the same. And I said, if any two things are equal to the same, then their hash code should also be the same. And that string must have made sure. That means, if I make any hash map, and once I pass A and once I pass B as key, then I am 100% sure that their hash code is going to be the same. That means, they are going to go in the same bucket. I am 100% sure of this. But all this happens when I use my normal HashMap. We have a special type of HashMap in Java which I call Identity HashMap.

  177. Speaker 1

    Which breaks this definition.

  178. Speaker 2

    Which says that for me, a and b will not be equal until a and b point to the same object. So in this case, where there is an a reference variable pointing to this object and a b reference variable pointing to this object. So basically, our identity hash map says that I don't care about the logical equality, I care about whether both a and b point to.

  179. Speaker 1

    The same object.

  180. Speaker 2

    If they do, then they are the same for me. But if they point to different objects, then whatever their value is, for me, they are different objects. This means that it works on equal operator. And we know which operator is equal operator, which compares references. Right? So just assume that if we use identity hash map, then for that, both A and B are different strings.

  181. Speaker 1

    They are not equal.

  182. Speaker 2

    Because it does not compare it with A.equals B. It compares it using A equal equal B, something like this. Now you will ask, what is its use case? There is a use case. There are very specific or rare use cases of it. When you talk about graph algorithms, like if you want to do cycle detection or you want to find topology, etc. Now these are very specific graph algorithms. If you don't have an idea about it, then it's okay. Identity hashmap is used only for those specific purposes. Otherwise, there is no specific use case for it. Just remember that in normal hashmap, we use equals to find out if two keys or two strings are equal or not. But in our identity hashmap, we use equal equal operator. And you know that equal equal operator compares references and equals compares our exact value. Apart from this, we have an enum map.

  183. Speaker 1

    Enum map. Now, what is this enum map? Look, this is interesting.

  184. Speaker 2

    Imagine you have an enum, day. So, you have made an enum of day. And in this, you have defined some values like Monday, Tuesday, Wednesday, etc. So, now if I tell you that you have to make a map in which the key will be an object of this enum. So, you will say that it is very simple.

  185. Speaker 1

    I will make a map.

  186. Speaker 2

    Let's say day. This day. comma let's say integer ka kuch iss tarike se mp equal to let's say new hash map aap normal apna hash map bana doge hai naa koi dikat nahi hai ye chal jayega aap chaho to isme value dal sakte ho let's say mp dot put aapne dala monday comma 1, something like this. 1.mp.put Tuesday, comma 2, something like this. Now, what are these integer values? 1, 2, these are nothing. These are arbitrary values that I have taken. Basically, I just wanted to tell you that if you make a map in which the key is a kind of enum, right? It has the objects of the enum. And the value can be anything, integer, string, anything. So generally you will prefer to make a normal Hedge PAM, right? Here this work will be done, everything will be fine. But it will be a little unoptimized in comparison to Enum Map. Java gives you a specialized map, Enum Map, which says that if you want to represent a situation in which there is a key Enum, then use Enum Map, it is more efficient, more optimized.

  187. Speaker 1

    Now see how?

  188. Speaker 2

    Java already knows that in an enum there can be a limited number of values and every value has an ordinal. Do you know what an ordinal is? It's a number. Let's say its ordinal is 0 on Monday, 1 on Tuesday, 2 on Wednesday and so on. So basically we have a finite number of set and everyone has a unique ordinal value. So by using this knowledge, Java, which makes buckets, can do better distribution of our keys.

  189. Speaker 1

    Right?

  190. Speaker 2

    The chances of collision in that are almost zero.

  191. Speaker 1

    Almost zero.

  192. Speaker 2

    But if you use the hashing technique, normal hashmap, then what will it do? It will remove the hash code of this enum and modulo it with our size which was n. So our value will go to one of these, our key will go to another. The chances of collision will increase but if we already know that we have an enum. So java knows that there will be limited values and everyone.

  193. Speaker 1

    Will have an ordinal. So it can use that as an index.

  194. Speaker 2

    If it can say that the one whose ordinal is 0 always comes to the 0th index, the one whose ordinal is 1 always comes to the 1st index. So something like this. Basically, it also does some other optimizations, but what I mean to say is that Enum Map is a much better way to represent if you ever want to use the values or objects in Enum as a key. Now, if you use Enum Map, then you have to remember that it doesn't use the hashing technique. It doesn't use the hashing technique. Right, it uses ordinal values and their indexing. It is more memory efficient than our normal hash map. Memory efficient. And it doesn't allow any null keys. Null keys are not allowed. Not allowed. Like HashMap allows one null key. It doesn't allow any null key. But null value allows. Null value are allowed. Okay. So basically you can write something like this. Let's say map of day comma integer. Map. Equal to new And instead of hash map you can simply write Enum map Something like this And after that you can put values in this map Like you normally put map.put Monday, 1 Map.put Tuesday, 2 Whatever values you want to put Whatever your business logic is In fact the most interesting thing about this is That in this iteration order is also preserved Iteration order is Preserved. Which doesn't live in normal hash map. Do you know what happens in normal hash map? Normal hash map. Because we use hashing from inside. Right. So basically. Whenever you iterate on it.

  195. Speaker 1

    So whatever.

  196. Speaker 2

    Your key comma values. Are obtained.

  197. Speaker 1

    Be found in any order.

  198. Speaker 2

    It is not necessary that you get it in the order in which you put it. Right? But enum map, because it is using the values of enums and it is using their ordinals, then you will always get the iteration order the way your enum values will be. So first Monday, then Tuesday, then Wednesday and so on. So, basically, if you use EnumMap, then your iteration order is also preserved. Now, the last thing we talked about in the starting, is our concurrent hash map. Look, this is our latest thread-safe map. If you know that you want to work in a multi-threaded environment, where race conditions can occur, and you want a thread-safe map, then you will use concurrent hash map. Right, you know that we don't prefer to use hash table. It has a different overhead which is very slow. We prefer to use concurrent hash map. It is very optimized. It doesn't lock your entire map. If you want to read any value or if you want to update any specific thing. Okay, so these are all the types of maps that we read in Java. Generally, you will use simply a hash map. Sometimes you will use a linked hash map. Linked hash map. And sometimes for specific purpose you can use tree map Apart from that, all the things we talked about Apart from all these, you can also use concurrent hash map If you are talking about a multi-threaded environment Rest all the things we talked about Enum map Identity map Identity map They have a very specific use case for a particular purpose. You will use Enum map only if you want an optimized map of Enum. There is very less use case of Identity map. If you want to study almost a graph algorithm, you can use Identity map for that. Rest, as you know, you don't use weak reference maps anymore. We don't prefer hash table, use it at all. You must have got a good idea of everything, you must have enjoyed it. This is it for this video. See you in the next video. Till then, thank you so much.