Podcast Lookup

Java Collections Framework Explained | Complete Hierarchy, Interfaces & Data Structures

The Java Developers Hub

  • December 3, 2026
  • 1 h 16 min
  • Episode 27
Episode page
About this episodeThe Java Collections Framework is one of the most important parts of Java programming. It provides a comprehensive set of interfaces…
The Java Collections Framework is one of the most important parts of Java programming. It provides a comprehensive set of interfaces, classes, and algorithms for storing, organising, retrieving, and manipulating groups of objects efficiently.In this episode, we explore the Java Collections Framework from the ground up, breaking down its complete hierarchy and explaining how its major interfaces and implementations work together.You'll learn about Lists, Sets, Queues, and Maps, understand their differences, and discover which data structures are best suited for different programming scenarios.Whether you're a beginner learning Java or an experienced developer preparing for technical interviews, this tutorial will give you a solid understanding of Java's collection architecture.What You'll Learn
  • What the Java Collections Framework is and why it matters.

  • Understanding data structures in Java.

  • The difference between Collection and Collections.

  • Understanding the Java Collections Framework hierarchy.

  • Introduction to the Iterable and Collection interfaces.

  • Understanding the List interface and its implementations:
    • ArrayList

    • LinkedList

    • Vector

    • Stack



  • Understanding the Set interface and its implementations:
    • HashSet

    • LinkedHashSet

    • TreeSet



  • Understanding the Queue and Deque interfaces.

  • Exploring PriorityQueue and ArrayDeque.

  • Understanding the Map interface and its implementations:
    • HashMap

    • LinkedHashMap

    • TreeMap

    • Hashtable



  • Differences between List, Set, Queue, and Map.

  • Choosing the right collection for different use cases.

  • Understanding collection performance and time complexity.

  • Best practices for working with Java collections.

Transcript

From the publisher

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

Java Collections Framework Explained | Complete Hierarchy, Interfaces & Data Structures

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java series. And today we are going to talk about one of the most important topics of Java, which is Java Collection Frameworks. Now Java Collection Framework is very important by itself, but there are many multiple things in it, like Java Streams, which we will see later. But let's start with its first video and move on to the screen. First of all, when we write code, we deal with data, right? Now obviously we put that data in object, in object oriented programming, but actually we deal with data only. Now whenever you write code, you have to store data in different ways, right? And you store data in different ways so that you can query data properly. So we have to perform a query as well. So from here the data structure is born. Now data structure is.

  2. Speaker 2

    A branch in itself. It is not deeply related to programming language.

  3. Speaker 1

    Because every programming language has a concept of data structure. If you know, we have arrays, right? This is also a data structure concept. Apart from this, we have many different types of data structures like link list, trees, hash, map, etc. But the most important question is why do we need data structures? And the answer is so that we can efficiently store data and query on that data. For example, if I ask you that let's say we have an array and that array is of normal numbers. So let's say I have stored some.

  4. Speaker 2

    Numbers in it. 3, 7, 10, 1, 4, and let's say 12. Something like this.

  5. Speaker 1

    And what I did was, I took a simple array and stored those numbers. But now I have stored the numbers. In fact, it is very easy for me to enter any new number. Let's say someone asks me to enter 24 in this array. I entered 24 in the last. My work is done. Tomorrow, someone else will say, let's enter 0 in the last. I will enter 0 in the last. Right.

  6. Speaker 2

    So, basically, it is very easy to enter a new number in this. I can do that in constant time. Constant time.

  7. Speaker 1

    Right.

  8. Speaker 2

    Now, what is constant time?

  9. Speaker 1

    If we call it on the basis of time complexity, then its time complexity is order of 1. Right. But, you think, if I tell you to do a query in this, I have a query, that I have to tell you, which is the largest number in this. So if I want to tell the largest number, then I have to go to every element and check if it is the largest. So I can do this by considering the first element as the largest, 3. Then if I get a bigger number than 3, 7, then I update the largest to 7. If I get 10, then I update the largest to 10. And in this way, I keep moving in the loop. Finally, I will get 24 largest elements and I will return my 24 in this way. But to do this query, how much time did it take me? I had to scan all the elements one by one. So it took as much time as the number of elements. And what do I call this in the language of time complexity? I thought order of.

  10. Speaker 2

    N time, right? n is the size of the array.

  11. Speaker 1

    So, the more my size of the array increases, the more my time will increase because I will have to ask every element one by one.

  12. Speaker 2

    Very good.

  13. Speaker 1

    So, basically, I made it easy to store the data. Constantly, I can add elements in it. But my query in the data became very difficult. So, my data structure is not that efficient. This means that storing the array in this way for this particular query, it is not that optimized, it is not that efficient, we have to think of something else.

  14. Speaker 2

    So what can we do?

  15. Speaker 1

    Look, if we apply the first thought principle, then it will come to our mind that what if we sort the array? Sorting means, we can keep it in ascending order or descending order. So let's say if I want to keep this array in sorted form I can say that I will convert this array in this way Let's say I am sorting in ascending order, so first the smallest number will come and then it will increase So the smallest number is 0, then our 1 and I will keep doing this in this way I have sorted all the numbers, first came 0 and last came 24 Now if someone asks me by doing the same query, that tell me the largest number, then can't I tell it in constant time? Where will I get the largest number? In the last. So basically, whatever function I write, what will I have to do finally? I will have to return finally, in array, this array of mine, which index? Array.length index, length-1 index, something like this. As soon as I return it, it means I am returning 24, which will be the largest.

  16. Speaker 2

    So what did I do?

  17. Speaker 1

    I changed a little bit in my data structure and stored my data in this way so that my query becomes very fast. So I said I used the sorted array. But now there is a problem in this sorted array too. Now my search is in order of 1. But suppose I want to add to a new element. Let's say someone gives me 6. If I add 6 to this list, I can't add it randomly at the end because I have to maintain a sorted order. So I have to see where 6 can come. 0, 1, 3, 4, 7. I think 6 should come between these two. 4, 6, 7 so that it remains in the sending order. Now what do I have to do to check this? Again, I have to go to all the elements one by one. I have to see which element is.

  18. Speaker 2

    Just bigger than 6.

  19. Speaker 1

    So I will get 7 just bigger than 6. So now from 7 to 24, I will shift each element to the right position so that I have space for 6 and I will insert 6 in between. Now we can write the code by ourselves, but that means my insertion is a bit complicated. Now how complicated it is, again it is in the order of n terms. Because in the worst case, I may have to push all the elements for once. So we took a trade-off here. If our maximum query is searching, then we said, no problem, let's write it in sorted form. Insertion will happen sometimes, and if it happens, then it will take order of end time. So what is every data structure? It's a trade-off. No data structure is better than anyone. It's just for our particular query, which data structure is most optimized. Now, we talked about data structures, and till now we talked only about arrays, which we had declared in our Java till now. But apart from arrays, we have multiple types of data structures which are also language independent. But Java in general provides support for almost all data structures. It says that I will give you classes, you can directly interact with those classes, so you can interact with those data structures. But suppose if Java or any programming language does not give us support, then can we make it ourselves?

  20. Speaker 2

    Yes, we can.

  21. Speaker 1

    With our existing knowledge, we can create multiple types of data structures. Because data structure is a way of storing data. Now, if Java doesn't support us, or if any other programming language doesn't support us, then what do we generally do? So, Java has given us a very interesting support, which is user-defined data types.

  22. Speaker 2

    And we know what user-defined data types are.

  23. Speaker 1

    We can use classes and objects to define our own data types.

  24. Speaker 2

    So, let's assume that Java.

  25. Speaker 1

    Has only given us the support of array and not the support of any other data structure.

  26. Speaker 2

    Which is actually given, whatever we read. But let's assume that this happens.

  27. Speaker 1

    So, what do we do? We make different classes of it. So, for example, there is a data structure called link list. And if Java had not given its support, we would have made it ourselves. We would have made class link list. Linked list, something like this. And we could have done whatever functionality we wanted to define. Right. But Java has still given us a very good support of all these data structures, which we call Java's collection framework.

  28. Speaker 2

    And all these.

  29. Speaker 1

    Data structures together make our work very easy in terms of how we have to store this data or in terms of which query we have to perform on it. So, let's quickly see all the existing data structures and see what kind of support exists in Java. So, first of all, I will talk about a dynamic array. Look, obviously Java has given us a simple array. But it has some limitations.

  30. Speaker 2

    What are the limitations?

  31. Speaker 1

    Now, first of all, I see its limitation that it is of fixed size. So, for example, I want to make an array of integer. So, let's say I make integer arr Equal to new int. Something like this. And I have to tell its size here. And suppose I put 10 first. So it means I can add 10 elements to it. And its index will be from 0 to 9. This we know. So now suppose I have this array which exists. Whose size is 10. So I can add 10 elements to it. Now if I have to add an 11th element. So can I do it directly? Absolutely not. In fact if I write arr of 10 like this. Because ARR of 10 doesn't exist.

  32. Speaker 2

    0 to 9 exists.

  33. Speaker 1

    So as soon as I write something outside the index, I get an error. Index out of bound exception.

  34. Speaker 2

    We know this.

  35. Speaker 1

    So, we get an exception like this. Now, again, its size is fixed, so we can't add new elements to it.

  36. Speaker 2

    So, we can already.

  37. Speaker 1

    Retain a very big limit. I have already written 1000. So, I made such a big array, so that I know that there are not going to be.

  38. Speaker 2

    More than 1000 elements.

  39. Speaker 1

    So, we can use it. But then, how much space are we wasting? So, there is a concept of dynamic array, which says, that there should be an array which has no fixed size. As we add new elements to it, it keeps on increasing. This is the concept of data structure. Has Java given us its support directly? The answer is yes. But let's assume we don't get its support and we have to create our own dynamic array. So think about it, apply the first thought principle that how can we make it using the existing array that we have. If we want to create a dynamic array and Java doesn't support it, then we will create our own dynamic array class. We will create our own user defined data type, dynamic array. What is the main principle of dynamic array and then we will build that class. So, the main concept of dynamic array is that let's say I have an array. It has a fixed size. Let's say 1, 2, 3, 4, 5. So, 5 elements can be stored in it. 0, 1, 2, 3, 4. Something like this. I am saying that dynamic array is nothing. What I will do is internally I will keep a normal fixed size array. I will determine its capacity from the beginning. Let's say 5 or 10. Whatever I want to keep. And I will give a method to the user. Let's say here I have given it a method add. Whenever it will call this add method, and where will this add method be?

  40. Speaker 2

    It will be in this dynamic array class.

  41. Speaker 1

    Whenever it will call the add method and give an argument in this method, whatever element has to be added, it will be able to add that element.

  42. Speaker 2

    So let's say it wrote add 4. What will I do? I will add my 4 here.

  43. Speaker 1

    Then it wrote let's say add 10. I will add 10 at the end. In this way, I will give it a method, add. Now, how long can it add? Until my array is not full. So, let's say it added 13 here, 15 here, 17 here. In this way.

  44. Speaker 2

    Now, as soon as it added, let's say it added 13. Add 15. Add 17. See, it added all these elements. Now, let's assume it writes add again.

  45. Speaker 1

    21, something like this. Now see, what we will do, as soon as it tries to enter 21, we will see that our array is.

  46. Speaker 2

    Full, we will not be able to enter 21.

  47. Speaker 1

    So internally what we can do, we can make a big array, we will make a new array. Okay, how much will we keep its size? We will double the size of the first array, so let's say we will make it 10, 1, 2, 3, 4, 5,6,7,8,9 something like this and if I write its indexing 4,5,6,7,8,9 so its size is 10 now what I will do is I will copy all the old values in new array so here it is 4,10,13,15 and 17 now see my other spaces are empty again now if I want to put 21 I will say 21 here I put 21 So even after 21, a user can add more elements.

  48. Speaker 2

    To the space.

  49. Speaker 1

    Let's say if the spaces are filled, let's say he added an element here, here and here. Now I will do the same process. I will double this array in size. And then again, I will copy my old values to the new array. And the user will get many more fields to add. So this concept of dynamic array can be done. Internally, this is my conceptual overview. And I can also write the code of this here. You must be able to understand how simple the code will be. Let's say I define a private int in our class. Let's say ARR. Something like this. Now I am not telling its size. Let's say I will make its size in constructor. So let's say dynamic array. I am writing DA right now. And let's say I will take the initial size from the user. And what I will do? ARR equal to new int. And I will make an array of that size. Something like this. Okay.

  50. Speaker 2

    So, basically, what does my arr hold? It holds an array.

  51. Speaker 1

    How many elements are there? Now, there are size elements. And obviously, the dynamic array, I am making it for integer. So, I have written private int arr. But if you want, you can make it generic. You pass t here and keep its datatype t. So that some string array or boolean array can make any array internally.

  52. Speaker 2

    Okay. So, I will do this.

  53. Speaker 1

    Now, what did I say? I will give it some method. So, let's say, I gave one method. I gave add. Okay. Now, you know, this method internally what will it do? This method internally will do the same thing that we talked about here.

  54. Speaker 2

    What will it do internally? It will maintain a.

  55. Speaker 1

    Counter and it will see that I have to add an element which is my last space available. If suppose my last space is not available, let's say till here 17 was filled and my counter is standing here that the array is full. In that case, I will first create a new array, copy the old values to the new array, and when new spaces will be available, I will enter the user values there. So again, I can implement this by myself. Now you will say that apart from add, there can be more functionality. Absolutely there can be. You can take an index here, getIntIndex. And you will return it that what is written in that particular index. Why?

  56. Speaker 2

    Because your main array is private. You will keep it private.

  57. Speaker 1

    You don't want me to give it access to your internal array. You want it to access through methods. Right? You want to maintain the entire encapsulation there. So you have given add a method. Which will take an element. So I didn't take an element here. So it will take here let's say int element. Okay? And you will add it in the last. One more method will be our get. Which will take an index. And will give whatever element will be in that particular index.

  58. Speaker 2

    Apart from this. You can imagine yourself. You keep one.

  59. Speaker 1

    Insert.

  60. Speaker 2

    What will insert do? If you assume.

  61. Speaker 1

    Don't put anything in last. Which ad was doing by itself. Insert will say. If you want to enter something in between. Then what can you do?

  62. Speaker 2

    So in that case. You can take two things. Int index. Where you want to insert. and int element, whatever element you want to insert.

  63. Speaker 1

    Okay? And how can we think about it conceptually? Suppose I have this array for now.

  64. Speaker 2

    And I want to insert 25. And where do I want to insert?

  65. Speaker 1

    Let's say, I want to insert in the first index.

  66. Speaker 2

    Okay? So, I'll write it correctly.

  67. Speaker 1

    So, suppose I said that I'll call this insert method.

  68. Speaker 2

    Insert. And what do I have to insert?

  69. Speaker 1

    First, the index will come, so let's say we have to insert the first index And then the element will come, let's say we have to insert 25 So what will I do? I have random access in the array, I will go directly to index 1 I have reached, this is my current array, I have reached this index 1 because this is 0 and this is 1 Now as soon as I reached this index 1, I will have to put 25 here But can I put 25 directly here? No, then my 10 will be replaced Insert means to insert a new element, not to delete an old one, not to replace If you want to replace, then you can make a new method, replace which will remove this 10 and add 25. But I don't want to do that. So what will I do next? From 10 to 21, I will shift the position of all the elements to the right.

  70. Speaker 2

    So this 21 will.

  71. Speaker 1

    Come here, this 17 will come here, this 15 will come here, while doing this, this 10 will come here. So this block will be empty and I will add my 25 to this block. So I can write code like this. Again, I have to check one thing here as well. Suppose my array is this which is almost filled. Or in this array also I have filled all the elements So 21, 31, 41, 51, 61 Something like this my array is filled Now if someone says insert 25 So if I want to shift each position right So I should have space for new position So again I will have to.

  72. Speaker 2

    Do the same work that I did in add.

  73. Speaker 1

    I will create a new array which will double the size of the previous array. I will copy all the values in the new array and then I will write my shifting code. So you can understand it yourself that if someone asks me to implement a dynamic array, can I do it? The answer is yes, I can do it. I have existing knowledge of classes and objects and I have the array that exists. You can write the delete method or any other method that you want to provide support on the array. And you can write it by yourself. But do we need to create this code every time? Whenever we need a dynamic array in Java, will we create it by ourselves?

  74. Speaker 2

    The answer is no.

  75. Speaker 1

    Java gives us its support very well. And we call it ArrayList in Java.

  76. Speaker 2

    What is ArrayList?

  77. Speaker 1

    Java has given us a very good class by doing array list. Which basically can do all the work. Which can do dynamic array. In which you can add, insert and delete elements in between.

  78. Speaker 2

    Etc.

  79. Speaker 1

    So we don't need to make any class by ourselves. Java has given us a class by doing optimized array list. Now, let's move on to our next data structure. So, first, let's say I discuss a problem with you that why we need that new data structure. Let's say the problem is that I have to do a lot of insertions. In our old data structure, dynamic array, there was a problem that this work is doing very well. But you think, whenever I insert, what do I have to do? All elements have to shift each position right. In which time takes a lot. It takes order of n time. Again and again shifting. And suppose my insertion.

  80. Speaker 2

    Is very frequent.

  81. Speaker 1

    Suppose the code you have written. It is happening that you have to insert things again and again. Its use case is like this.

  82. Speaker 2

    Okay.

  83. Speaker 1

    So which data structure will you use? Now you get a new data structure. Which we call linked list. Link list. Now what does this data structure do? First I will give you a conceptual overview if you don't know. Now see, link list says that an array stores data inside a contiguous memory. So let's say I made an array of 4 lengths. So if I want to store data, 2, 3, 4, 5, then all these memories, 1 block, 2 blocks, 3 blocks, 4 blocks, are contiguous memories. Contiguously, if its address is 1000, then its integer will be 4 bytes, so you can add 4 bytes to its current address. So you can add 1004 if I am talking in bytes form and 32 bits if I am talking in bits form. So basically, all addresses come in contiguous memory location. So the problem is that when this array gets full, I can't add new elements because I don't have any other memory. So I'll have to allocate a new memory again, double the one we saw above. I'll have to copy the old value, only then I'll be able to add new elements. What if we forget the contiguous memory? I'll write a node, now imagine what a node is, a memory location, okay? My data is 2 in that. Now let's say I want to insert 3, so I can create a new node in memory location where I insert 3. Now these memory locations can be different. Let's say this is 1000 memory location and this is 2000 memory location. But what will be the bond between these two? How will we know that we have inserted 3 after 2? For that, I will put a reference variable of this new object in this 2.

  84. Speaker 2

    So it will point this.

  85. Speaker 1

    In the same 3, I will keep a new reference variable of a new node in which we have inserted 4. And same goes for a new reference variable for 5 in which we have inserted 5. Something like this. This is the concept of a linked list. Maybe you have read this in the data structure. If you haven't, then it's very simple. You can imagine that if I make it properly class-wise, then you can imagine So what I would have done is, I would have made a class, like I would have named that class node. Okay? Let's say node, because what does node mean? It can store a data. Now what can happen in that? Let's say it can be data, so I have taken a variable, data. Right? Now again I have taken it as an integer type, you can make it generic if you want. So basically what it means is that in an object of node, so if I make an object of node, a data would be stored in it, like I have filled it here, 2, 3, 4, 5, something like this, and what will happen in it? There will be a reference variable of another object. And what type of object will it be? Same, it will also be of node type. So I can do this, I can write node. And I keep the reference variable name as ref. So what I did is, I kept an object of the same class in the same class. And this is a very interesting thing. We haven't seen this before in our Java syntax. That we keep a reference variable of the same class in the same class.

  86. Speaker 2

    And we can do this. And that's it.

  87. Speaker 1

    I can work like this. Now imagine this class. What will happen if I copy this class?

  88. Speaker 2

    So see.

  89. Speaker 1

    I have copied this class again. Now you can see that I have a heap memory and I have a stack memory. Now let's say I want to make an object of this class node. How can I make it?

  90. Speaker 2

    I can write node.

  91. Speaker 1

    Let's say node n1 is equal to new node. So I write n1. data and add value of 4. So I made a new object n1. So you know where this object went. This object will be in our heap memory. And its reference variable is n1 which will be in my stack memory. And n1 will point this object. Now its data part is 4. So you know there will be a data here. In that there will be 4.

  92. Speaker 2

    Right?

  93. Speaker 1

    And again a reference variable ref will also be there in it. And which type will it be? Of node type. That means this object can point to another object of this class. But now I have not put any value in it. So by default what will be there here? By default null will be there.

  94. Speaker 2

    Something like this. You can assume. Now assume I make it a new object.

  95. Speaker 1

    Node. n2, n2, and equal to, I write new node, okay, again, what will happen here, Let's say a new object has been created in my heap memory which will contain data and ref. And I will point it to reference variable n2.

  96. Speaker 2

    Let's say n2.

  97. Speaker 1

    Data is equal to 5, so I will write 5 on the data point. And by default, reference will be null. So far, these two objects were independently made.

  98. Speaker 2

    Now, how can I connect these objects together? With just one line.

  99. Speaker 1

    Now imagine if I write that this reference variable n1, dot, the reference variable in it, r, e, f, if I put a value in it, whose?

  100. Speaker 2

    Of n2.

  101. Speaker 1

    So basically if I write n1. ref equal to n2. So this means that I am saying that n1. ref means that in this reference variable there should be that value which is referring to n2. Because see what is ref? It is a node type variable which means it is a reference variable. And what can come in it? An address can come in it. So I am saying that put that address in this ref which is in n2. Now n2 has the address of this object. So what did I say? I said Add its address in this ref as well. So let's say I will do something like this.

  102. Speaker 2

    I will link it here.

  103. Speaker 1

    So according to memory, I have an address of this object in this reference variable. Okay, I hope you understood. So isn't it similar to the one I made a link list on? See, I had kept 2, 3, 4, 5 here. And what were all these? They were all no-type objects. So let's say this is n1, this is n2, this is n3 and this is n4. And I had written that put n1 in n1.

  104. Speaker 2

    Ref. n2, here I would have written n2.

  105. Speaker 1

    Ref.

  106. Speaker 2

    N3, and I would have written n3. ref.

  107. Speaker 1

    N4. And N4' s ref is null, so it doesn't point to anyone. But it looks like this. 2 points to 3, 3 points.

  108. Speaker 2

    To 4, 4 points to 5.

  109. Speaker 1

    And how can I represent this in memory?

  110. Speaker 2

    Something like this. I can say that this is my N1, N1 points to N2.

  111. Speaker 1

    Now let's say I'll put one more in this, I'll put one more in this, and where will all that be? In my heap memory. So this memory that I make, I call this link list. Because I can put an element in a node and link it with another node. And I can scale this infinitely, there is no limit. Because I haven't taken a contiguous memory location. So again, I have a new data structure called a link list. Which gives me the benefit that I can insert as many as I want. My link list is never full. Because the dynamic was getting full from inside. We were making people appear like this. Whatever it was using, it was never full. But actually what we used to do is, as soon as the array was full, we used to make it double from inside. But that was a little costly operation. But we don't need to do this in the linked list. Whenever we need to add something in the linked list, we just need to make a new node. And whatever old chain was coming, we will have to link it with it. Something like this. So suppose I had some elements from before, 4. One was 5 and the other was 6 and they were already linked to each other Someone said I want to add 7, so you would have given a method add and it would have passed 7 You will not do anything, you will make a new node and put the value 7 and link it with this, something like this Okay? So this is the concept of a linked list. Theoretically, you must have understood. Now, if the question arises that can you make your own linked list in Java? The answer is yes, you can make it. Can you make it with the knowledge that you have till now? The answer is yes. Now, what have we done? We have made a node class, right? Which can store a data and a reference. And what is a node class? In some way, its object is made in which we have a data. And what will happen here? A reference variable of another object will be there. Now, if we want to make a linked list class, let's say, we want to make our own class. Link.

  112. Speaker 2

    List.

  113. Speaker 1

    And again, I want to keep this class abstract like an array class. Abstract means that the user doesn't know how it works from inside. The user has to give some operation add, which will call, let's say it said add 4.

  114. Speaker 2

    And what we will do is add 4 in our list. Something like this.

  115. Speaker 1

    Then it can say from inside, let's say add 5. So we added 5. Let's say it added 6.

  116. Speaker 2

    And added 7.

  117. Speaker 1

    So let's do something like this from inside. I want to tell the user what we are doing from inside. Again, we can give the user an insert function itself. Insert 8 and where to insert? Let's say, insert in position 1. So, again, although you don't do any indexing in the linked list because it is not one after one.

  118. Speaker 2

    But still, you can assume one.

  119. Speaker 1

    Position as 0, this is 1, this is 2, this is 3. In this position, I will add a new element. I want to add 8. Now do I have to shift all these again? The answer is no. What I will do is very simple. I have to add 8 in the first position. I will make a new node.

  120. Speaker 2

    Let's say 8.

  121. Speaker 1

    Something like this. And this old node that I had.

  122. Speaker 2

    I had this old node.

  123. Speaker 1

    I will break this link and I will say now 4 will point to 8 and 8 will point to 5. So see what happened to me, all my links got attached again, something like this.

  124. Speaker 2

    Now 4, 8, 5, 6, 7, my link list became 12 again.

  125. Speaker 1

    So this is the benefit of the link list that if I want, then how many elements do I add at the end, the link list can never be full. And if I have to insert something in between, then I can do it easily, I don't have to do any shifting. So again, my major question was that if my The problem statement is that there are a lot of insertions. Either in the last or in the front. So, in that case, link list will be very helpful. Because my complexity will be reduced. I won't have to shift elements again and again. So, how can I provide that? I can do a class called link list. So basically, I had to give all these functions to the user, add, insert, delete, replace, etc. But if we think of a basic structure, then how can we make a linked list class? Just think a little carefully. I said that we also need a node class, this one. We need a node class to store the data. Apart from that, we also have to give operations like add, insert, etc. We had learned the concept of nested class, right? We know that this node class above us, its sense is made only in a linked list class. We don't need to keep it out. And secondly, we don't want to keep it out because we want to keep it private in a linked list class. So that no user can access it directly. We want to give add, remove, these functionalities to the user. But we should have the responsibility of that node class. So can't we do something like this? If I create a private static I will make a class and name it node which we had made above. And in that we have data and reference pointer of this node. Now when we talk about linked list in data structure then I generally call the reference pointer next. Because it is pointing to the next object. So what I did here is I made an internal class. I made a static nested class. Now I can use this static nested class to give some of my functionalities. Let's say I have given a function public void add which will take an element int element and insert that element in the last in my linked list.

  126. Speaker 2

    What will it do?

  127. Speaker 1

    By default I will keep here a reference variable head.

  128. Speaker 2

    Something like this.

  129. Speaker 1

    Initially, I have a reference variable here, head, which is of node type. So, suppose I have a reference variable named head which will be null initially. So, again, where will this head reference variable be? It will be in our stack memory. So, suppose this is my stack memory. Then, where is my heap memory? My heap memory is something like this. So, as someone said add 4, what will we do from inside? First, we will make a new node. We will put a value of 4 in it. And we will add null to the reference because it can't point to anyone. And I will say that the reference variable head will point to this new node. Suppose someone asks to add 5, what will I do? I will create another node in this heap memory which will have a value of 5 and the reference will point to the null. But now my responsibility will be to put the address of this node in the reference variable of the first object that the head was pointing to. So now there will be a story that the head points to this node and this node points to this node. Again, what will I do? I have written add 6. So I will create a new object with the value of 6 and the reference variable will be null. And now it will be my responsibility to point this 5 variable to this 6 variable. So now my list looks like this, 4, 5, 6. So I can do this easily. You can write the code yourself. You will do some brainstorming and you will definitely be able to write the code of a linked list class in which we have made the node class as an internal static class. If you want, you can make this class separately but again, it won't be necessary because the use of this class comes inside this linked list class and I have to give the user only abstract functionality.

  130. Speaker 2

    Like add, remove, insert, etc. I don't have to give.

  131. Speaker 1

    Him access to this node, so it's best to make it inside like a nested static class and implement all these functionalities. But this is the part of the whole data structure and again the question arises that we can do this in every language, C + +, Java, Python, in any.

  132. Speaker 2

    But if we want to use a.

  133. Speaker 1

    Linked list in Java, because I know that my code has a lot of insertions. And I want to use an internally optimized data structure for which I have a link list. So will I write this code in Java again and again?

  134. Speaker 2

    The answer is no.

  135. Speaker 1

    I can write it if I want, but Java provides me with a class and that class is also named linked list. If I want, I can call this class. In this class, I have all the methods that we were talking about making at the top. Adding, inserting, removing, replacing, etc. If I want, I can call these methods. Again, I don't even need to know how the linked list works from inside. Because all the methods are defined in this class. I just have to call those methods and my work will be done.

  136. Speaker 2

    This is the abstraction.

  137. Speaker 1

    I don't need to know how the linked list works from inside. But still, because we need to know the DSA, we need to understand how the linked list works. And if we want to make it ourselves, how can we make it? So we can make it like this. Now let's move towards our new data structure, which we call.

  138. Speaker 2

    Set.

  139. Speaker 1

    Again, this is a data structure. Every programming language implements it in its own way. Java has also implemented it. First of all, let's understand what is set. I am saying that suppose you have a use case and suppose the use case is that you have to insert a lot of numbers and duplicates are not allowed in those numbers.

  140. Speaker 2

    What does it mean?

  141. Speaker 1

    It means that you have to make a container type. The number I give you, you insert it. I gave you 2, you inserted 2. I gave you 3, you inserted 3. I gave you 5, you inserted 5. But as soon as I give you a duplicate number, I give you 2 again. So, you don't insert 2 again. Because you know that 2 is already there, so you leave it. Then I gave you 5 again, you didn't insert 5. So, let's say you have 2, 3, 5. Now, when I tell you to give me all the numbers one by one, you give me. Here, 2, 3 and 5. I have unique numbers. I should not have repeated numbers. Second, you can assume that I can do such a query anytime. That do I have 5 in this container?

  142. Speaker 2

    What is 5?

  143. Speaker 1

    And you have to give the answer in yes or no. And you have to give the answer in order of 1. That means in constant time. But if you imagine that If I try to implement all these functionalities in array, then there.

  144. Speaker 2

    Will be a problem. Duplicates are allowed in it.

  145. Speaker 1

    If you put it in array, then you will put 2, then 3, then 5, you will put it back and forth. And if you purposefully want that duplicate is not allowed, then whenever I give you a duplicate number, let's say I gave you 2, then you will have to go to every element and check whether I had 2 before or not. If someone says that 2 was already there, then you won't insert it. So I'll do the duplicate part through array.

  146. Speaker 2

    Okay.

  147. Speaker 1

    The next thing was that I had a query that I should give you an answer that whether I have a particular element or not.

  148. Speaker 2

    Although I can also do that with array.

  149. Speaker 1

    I can do it one by one and ask from every element. Suppose someone asked if there is 5. So I will compare here, is it 5?

  150. Speaker 2

    Is it 5? Is it 5?

  151. Speaker 1

    The answer is yes. So I will say yes, I have 5. But again, how much time will it take to do this? It will take order of n time.

  152. Speaker 2

    Linear time.

  153. Speaker 1

    Because I will have to go to every element and ask. So basically, by using simple array, I cannot solve these two problems. To solve these two problems, I need a new data structure, which.

  154. Speaker 2

    I call set.

  155. Speaker 1

    Now set obviously solves these problems and if you want you can make your own set using array and this is very simple. You basically have to change in some arrays so that these two functionalities are fulfilled that duplicates are not allowed and you can query in constant time. You can do these two functionalities possible using array.

  156. Speaker 2

    Okay, how can we do it? Let's see that.

  157. Speaker 1

    So, you imagine that I will tell you that first I will have a problem that I will give you some numbers. I will tell you to store 1, 2, 3, 5. And I can also say something repeated. For example, I said again to store 2, 3, something like this. And while doing this, I can give you any number. So, let's say whatever number I can give you, it is in the range of 100. So, I can give you any number in the range of 1 to 100. So, there is one way to implement set. If I want to make an array, array and what will be the size of that array? The size of that array will be 100. So, the first index is 0, the second index is 1 and by doing this, the last index will be 99. Right? Because 0 to 99 will be there if I have to store 100 elements. Now, this problem, see what was the first problem?

  158. Speaker 2

    Now, there are two things.

  159. Speaker 1

    In the set. Duplicates should not be allowed. Duplicates are not allowed. And second, in constant time, we have to tell element exist or not not exist I have to tell this in constant time. Now see how I can solve this problem. So as soon as someone says insert 1, do you know where I will insert 1? I will insert it on the first index. So that I insert it in constant time, I put 1 instead of 1. Now someone will tell me to insert 2. So where will I insert 2? On the 2th index. So this will be the 2th index. I will put 2 directly here. Right. So by doing this, I will insert every element in the same place where its index comes. Now as someone said 3, here 3.

  160. Speaker 2

    What did I do? Again I put 3.

  161. Speaker 1

    I have inserted it on the third index like this. Now, what will I do if someone says 5? Here, I will have 4th index and here 5th index. This is 4th, this is 5th. I will directly insert 5 in 5th index. So, basically whenever I have to insert something, then whatever value comes, let's say the name of this array is arr. I have only one type of code. So, let's say I have to make a function here.

  162. Speaker 2

    Add int.

  163. Speaker 1

    Element something like this and I can directly add that element to arr of el so if someone asked me to add 5, I go to arr of 5 and add 5 directly so what did I take for addition? I gave constant time, I am able to add in constant time now see my duplicate problem is solved, now someone asked me to add 2 again my code will run again, here will be 2, I will say add 2 to arr of 2 Again, I will put 2 in arr, so I will put 2 again after removing 2. So basically, every time only one 2 will go, no matter how many times 2 is replaced. Or if you don't want to replace it again and again, then you can put a condition here. You can check first that if there is already an element on that arr, means there is not equal to 0, then you will skip it. You simply return it from here. So you can do this too. If you want, then you can skip this operation as well. But again, this means that my problem is solved. Duplicates are not allowed. Now, in constant time, I have to tell if the element exists or not. This is very simple. Let's say I have a method called contains. And it will take the int element. And I just have to tell whether the element given by the user is there or not. So, let's say it will give 5 here. So, is it 5 or not? How simple is it? I just have to check that if in arr of 5, If it is 5, then the answer is true.

  164. Speaker 2

    If it is not, then the answer is false.

  165. Speaker 1

    So return arr of 5, means here I have to write arr of el is equal to equal el.

  166. Speaker 2

    If it is true, then return true. If not, then it will be false. Simple. So I did this again in constant time.

  167. Speaker 1

    In what time? In order of 1. These two operations are in order of 1.

  168. Speaker 2

    Very good.

  169. Speaker 1

    So, this work is also done. So, did I not implement it in the set form?

  170. Speaker 2

    Which I wanted.

  171. Speaker 1

    Yes, there is a small problem. Where will we insert 100? I have kept 0 to 99 here. So, I should have kept 0 to 100, not 0 to 99. This is my 100, so that I can insert 100 here. So if it can come from 0 to 100, then I will keep the length 101, not 100, so that I can insert 100 too. So we can see that a little bit. But again, I have solved the problem here. Now you can say, but this is when we have elements in the same range. Suppose I am telling you that the range can be anything, 1 can also come, 2 can also come, 100 can also come, 1 lakh can also come, 1 crore can also come, anything can come, no matter how big the number can come. Then what will you do? Then I have one more concept. What will I do? What if I keep an array of fixed size? Let's say in the starting I have kept 10 size. So see here I have kept an array of 10 size.

  172. Speaker 2

    From 0 to 9 is its index.

  173. Speaker 1

    Now I am saying that any element can come in any range. So this upper approach is not good. Now it is not like that I will make any big array. How much should I make?

  174. Speaker 2

    1 million, billion, trillion.

  175. Speaker 1

    How big should I make an array? And my memory will also consume that much. Because most of the elements may remain empty. Wherever it is time to add an element, the same element will be there. So again, this is my very unoptimized way. The better way is to keep a fixed size first. As soon as someone asks me to add an element, let's say someone asks me to add 1. So what I do is, every time before adding, I put a modulo operator on its array.length. Array.length means 10. So if someone wants to add 1, I will say 1. modulo array.length means what will be the answer of 1 modulo 10 now whatever answer I get, you know it will be in the range of 0 to 9 whatever answer I get, I will add it to that particular index now the answer of 1 modulo 10 will be 1, you know this so suppose I directly go to 1 and add it, something like this now someone will say, let's say instead of 2 someone said add 10 because the answer of 2 will be 2 now the answer of 10 modulo 10 is 0 so I will go to 0th index and add 10 something like this Now, you can understand that someone might have said, let's say 6. So, I will add 6 to 6 modulo 10, whose answer is 6. For now, let's assume that I have added 3 elements, 10, 1 and 6. 6 is on 6, 1 is on 1, but 10 is on 0.

  176. Speaker 2

    No problem. Now, let's assume that someone said to add 12 after 6.

  177. Speaker 1

    Now, we come to 100. But let's assume that we add 12. So, here it will be 12. And the answer of 12 modulo 10 will be 2. So, I will add 12 to the second index. Something like this. So again, my add functionality is working and doing it in constant time because this operation will take my constant time. But how will the functionality of contains work? Let's say contains int element and I have to tell in constant time whether any element exists or not. In exactly the same way, whatever element it will.

  178. Speaker 2

    Give me, I will check whether arr of.

  179. Speaker 1

    Whatever element it has given, length of the array, so arr.length, if Array of EL modulo ARR dot length equal equal is ELK.

  180. Speaker 2

    So return it.

  181. Speaker 1

    True.

  182. Speaker 2

    If not, return it.

  183. Speaker 1

    False.

  184. Speaker 2

    Same condition.

  185. Speaker 1

    Now suppose someone gave 12. Does 12 exist? I will do 12 modulo 10. What is it?

  186. Speaker 2

    12 modulo 10 means 2. So I will check ARR of 2. So I went to ARR of 2. Which is here.

  187. Speaker 1

    And I checked that there is equal equal 12 there. The answer is yes. So I will return true. So this also worked in my constant time. So again using this modulo operator, I can do this work too. I will have one problem here. Let's say someone said 100. Now I will do 100 modulo 10. So the answer will be 0 again. So here already 10 is there. And I can't replace 10. Why? Because this is not a duplicate value. I am not entering 10 again. I am entering 100. In that case, I will have to bring another new concept which I call chaining in the language of data structure. What is chaining? I would like to store 100 or more than 10. Now you can assume that if I make a link list of this 10, then basically I will store 100 in front of 10.

  188. Speaker 2

    Something like this.

  189. Speaker 1

    Now suppose it comes again after 100, it comes 1000. And if you mod 1000 from 10, it will come 0 again. So I will add 1000 in this list as a link list. And we call this chaining. Now what will happen because of chaining? There can be multiple elements on a particular index. There is one more after this, there is one more after this, there is one more after this.

  190. Speaker 2

    Something like this. Maybe there is a chain of my own here.

  191. Speaker 1

    Right?

  192. Speaker 2

    Something like this. But it's okay.

  193. Speaker 1

    That chain won't be too long. If the numbers are less. But if my chain is too long, then what can I do to optimize it? I can double the size of my initial set. Like I did in the array form. so that my numbers can be distributed more. Now, there will be some complications that I will have to pass all these numbers again through the modulo operator and give a value. But you understood the basic idea that how we can implement the set using array and link list. By using these two, we can implement our entire set. We have also discussed the first case in which we simply We put one number on its particular index, which is a bit impractical if the number is too much. And in the second case, we turn it into a modulo operator. But till now we were dealing with numbers, maybe you might have a question, what if someone puts a string? But again, till now we were dealing with numbers, maybe there are strings in our input instead of numbers. Let's say I give you a problem statement that anyone can enter their email id. You have to check if that email id already exists or not. Or you have to store the email id. And duplicate email id cannot be there. So in that case it will be set.

  194. Speaker 2

    And what is email id? Email id is in the form of our string. So how do we make a string a modulo? For that we have the work of hashes.

  195. Speaker 1

    We have already studied that in our object class, there is a method called hashCode.

  196. Speaker 2

    So what can I do?

  197. Speaker 1

    I can take out the hashCode of any string and the hashCode will always be an integer. I will mod that integer with my array.length which is 10. So where will its value come? It will come in this range, 0 to 9. So my work will also be done. If I want to enter a string or an object, then I can easily do it.

  198. Speaker 2

    I just have to enter their hashCode.

  199. Speaker 1

    Okay.

  200. Speaker 2

    But again, same question.

  201. Speaker 1

    In Java, do we need to do all this if we want to use a data structure called set? The answer is absolutely not. Java gives us some classes by itself, like hash set. And we have another one, linked hash set. Now, what is the difference between these two? We will understand it when we read it. But Java gives us the functionality of these two by itself. If we want, we can use these classes. And we will get the same functionality that we will be able to query in constant time whether any element exists or not. And duplicate element will not be able to enter. Java gives us the possibility of this by itself.

  202. Speaker 2

    Okay.

  203. Speaker 1

    Now, let's move towards our next data structures. Which are actually two data structures. Which we call stack and queues. You may have heard it very well before, it doesn't matter if you don't hear it. Now imagine that you have a use case that you have to store some elements again. So I will give you an element, store 2, store 3, store 4. And the order in which I will give it to you, when you return it to me, you will return it in the opposite order. So let's say I told you that first 2 went, then 3 went, then 4 went. So, you have stored it in a container. And when I ask you to return it, first you will give me 4, then 3, then 2. Assume you have such a use case. There comes our stack data structure. What does stack say? Stack says, assume you have such a container. You say, put 2. I put 2 at the bottom. Then you say put 3. I put 3 above it. Then you say put 4. I put 4 above it. So again, I put 2 at the bottom, then 3 and then 4. And the special thing about this data structure is that that entry and exit are from the same side.

  204. Speaker 2

    This side is closed.

  205. Speaker 1

    So if entry and exit are from the same side, then as soon as you put a new element, you put it from this side. If you put 5, you put it from here. And when you take it out, you also take it out from here. So again, you know that when it comes out, 5 can come out first. Because until 5 comes out, we can't take out 4. You can never take anything out of the middle. Entry will be from one side and exit will be from the other side. So we call this type of data structure as stack. Now you think about it, when I will remove it, first 5 will come out, then 4 will come out, then 3 will come out, and finally 2 will come out. So we call this as stack. In fact, we call stack in terms of programming as last in first out, LEFO. Its full form is last in, first out. And you know why it's called LIFO? How did we enter? 2, 3, 4, 5. And how did we go back? 5, 4, 3, 2. So the element that went last, that element came first. So last in, first out. This is called LIFO, this is called stack. How can you imagine? You can imagine a stack of plates. You keep a plate, then another, then third, then fourth. And when you take it out, first the top plate comes out, then below it, then below it, and then below it. Right? What is the way to implement this? It's very simple. You, again, you still have arrays. You can use arrays like a stack. So how can you do it? Suppose you take a fixed size array. 1, 2, 3, 4, 5. Suppose I took an array of 5 sizes. And this is their indexing. Now generally the class you will make, class stack. In that, you will use this array, right? So you will have an int arr, right? Now see, how can you express it in the form of a stack? It's a simple thing. You don't give me access to many methods. Like you don't give me access to insert. You don't give me access to replace. Basically, don't give me access to any such method. In which I can go to this array and insert an element somewhere in between. Because you know what happens in a stack? An element is inserted from one side and an element is removed from the other side. So let's say you give me access to only two methods.

  206. Speaker 2

    First, you give me add.

  207. Speaker 1

    So here I can add any element And give me a method called remove And I don't take any argument for remove Because I know if someone has said remove Then the first element will be remove No one can write like this That remove 5 Or remove 7. No one can tell me which number to remove. Nor can anyone tell me which index to remove it from. So I said remove 3. So no one can tell me this. They will just tell me remove and I will remove it from the top. Now assume that the top of my array is this.

  208. Speaker 2

    Which is 0th index.

  209. Speaker 1

    So again you can assume that this array This is represented as a stack, you just have to bring the stack like this. Where 0 is the first one and 4 is the lower index. So it will look something like this. Now someone will say add 5. What you can do is simply add 5 here. Now someone will say add 6. So again where will you have to add 6? You have to add it at the top. So this 5 will go down. So you will say 5, one step right shift. So 5 will come here. You will put 6 here. Now you know someone will say add 7.

  210. Speaker 2

    By one, 5 will go here, 6 will go here, and 7 will go here.

  211. Speaker 1

    Something like this. Now suppose someone says remove, let's remove it from here. So if someone says remove, then you will just bring 7 from here and remove 7. So 7 is removed from here. Now if 7 is removed, then 6 will come one step up. And if 6 is one step up, then 5 will also come up from here.

  212. Speaker 2

    Something like this.

  213. Speaker 1

    Now obviously this is very unoptimized because you have to shift all the elements again and again. Now the good way is that you tell yourself that this is the top and this is your bottom. Now you can do it like this, someone said let's say 6, so what did you do? You added 6 from the top, but you add it at the very end, because it came at the end of the container. Now someone said 7, you put it ahead. Someone said 8, you put it ahead. Someone said 9, you put it ahead. Someone said 3, you put it ahead. Now if someone says remove, you will remove 3 in this way. Then he will say remove, you will remove 9 in this way. For this, you can do one more thing Whenever you put the last element, let's say you put 8 So you can store 8 in a counter, in a variable Which will monitor that how much our stack is full So our stack is till here Now if you want, you can put two more elements in it And if someone will remove it, then you will remove 8 and give it to them There are many ways to implement this If you want, you can implement your stack in the link list too by yourself No problem So there are many ways So this is your stack data structure If we want to use stack then we will have to make it by ourselves, the answer is no. We have classes for this in Java as well. Again, there is a data structure Q which is the opposite of stack. If the stack was lefo, then Q is our fifo i.e. first in first out. Now imagine Q as a normal Q. What happens in Q? A person comes and stands in a line, a second person gets behind him, a third person gets behind him. Now when people start coming out of the line, the number of this person who came first will come first, then this person's number and then this person's number.

  214. Speaker 2

    So, Q is FIFO. First in, first out.

  215. Speaker 1

    So, the number of the person who stood in the queue first will come first. Now the way to think about it is that I give you some number 2, 3, 4 and when you return it to me, return it in the same 2, 3, 4 format. So the number I gave you earlier should come first. Now again, what is the simple way to implement this? You can use the stack. So the queue looks like this, you insert 2, so 2 comes here. Then you insert 3, 3 comes behind it. Then you insert 4, 4 comes behind it. That means insert is happening from one point, this is the point of insert. And removal is happening from the other point. Because now I will say remove So, 2 should come out. Then, I'll say remove again. So, 3 should come out. And when I'll say remove for the third time, then 4 should come out. Okay? So, you know, the stack is completely upside down. In the stack, only one side was being inserted and removed. And in queue, what are we doing? We are inserting from one side and removing from the other side. Again, I'll How can I express it in the form of an array? You can imagine that I have taken.

  216. Speaker 2

    An array of 5 sizes. So, 0, 1, 2, 3, 4.

  217. Speaker 1

    So, now it's very simple. You just have to do one thing. Assume that there will be removal from one side and entry from the other side. So, let's say to optimize it a bit, I will say that removal will happen from here. And your entry will be from here. So let's say someone asks me to push the 4 element. You imagine that it should enter from the entry side. So I assume that it has entered from the entry side and I have directly brought it here and removed it. Because why was the rest of my poori empty till now? Now why did I remove it here? So that its removal is done on a constant time. Now someone asked me to put 5, so I will put 5 behind it. Someone asked me to put 6, so I will put 6 behind it. Now someone will say remove. Till now it was being added. Till now the method was being called like this. Add 4, add 5 and add 6. After all this, the next method is called remove, which will not take any index. So what I will do is, I will simply remove 4.

  218. Speaker 2

    In the same way.

  219. Speaker 1

    Now if the next element again writes remove, Suppose someone wrote remove again, then the next element will be removed.

  220. Speaker 2

    The next element will be removed 5 because 5 was on top.

  221. Speaker 1

    If someone writes remove again, then this time it will be removed 6 because 6 was on top.

  222. Speaker 2

    Something like this.

  223. Speaker 1

    Now when you solve it with the problem of data structure, then there is more optimization in it. You will read that you can implement it like a circular queue to save more space and time etc. But we don't want to go deep into the data structure, we just want to take an overview that we can use the queue data structure and the stack data structure both as arrays or as I told you in the array, in the same way, it can also be implemented using the link list. But will we do it ourselves? The answer is no. If we want, we can make our own class stack or our own class queue. which does all these operations and works with an array from inside. Again, if you want, you can keep it generic instead of int type. But will we do it ourselves? The answer is no. Java has provided us with multiple classes which do the same work that we were discussing above. And what is the name of those classes? We have an interface, a queue interface. We have an array deck class. We have a stack of multiple types of classes or interfaces. We will understand it when we come to it. But again, we can call them. We have some operations in it and we can use those operations to do all the work that we have seen so far. Now let's move on to our next data structure which we call maps in programming. Now assume that you have to store some data of this kind which is key to the value.

  224. Speaker 2

    What does it mean?

  225. Speaker 1

    For every key, there should be a particular value. Now, this key can be anything and the value can also be anything. So, for example, whenever I talk about a key, I will talk about someone's name. And whenever I talk about value, let's say, I will talk about his roll number. So, I said, let's say, one person is Aditya, right? His roll number is 101. One person is Rohit, let's say, his roll number is 102. Let's say, Rohan's roll number is 103. So basically, I'm storing some information that I have a name and a roll number mapped with that name. So I have to store the information of this detail completely. And I have to store this detail in constant time, in order of 1. Now again, I can come and query from you that do I have Aditya's roll number? And you give a straight answer, and its answer is 101. And how much time should I give it? Again, give me a constant time. Okay?

  226. Speaker 2

    Simple.

  227. Speaker 1

    Tell me in a constant time. Aditya's roll number is 101. The first thing. Okay? And the second thing, again, like the set, no duplicate keys are allowed in it. Duplicate keys are not allowed. So if I tell you again, that take Aditya, and its roll number is 105. You will see that Aditya is already inserted, and the roll number is something else.

  228. Speaker 2

    I will not insert this. This is also not allowed.

  229. Speaker 1

    So this is almost like a set. But what was there in the set? There were only values. You used to put 1, 2, 3. In this, I am saying that there is a pair of key, comma, value. I am saying Aditya, comma, 1, 0, 1 or Rohit comma 1, 0, 2. So I'm giving you a pair. Now how can you store this or how can you resolve the query I told you? Which I told you should be resolved in constant time. This is where our map data structure works.

  230. Speaker 2

    Can we make our own map? Absolutely.

  231. Speaker 1

    You think, like we talked about a class till now, node class in the link list.

  232. Speaker 2

    Let's make a new class again.

  233. Speaker 1

    Okay. Let's say I call it node and there will be two things in it One will be our key, our key is of string type because it is a name And one will be our value, our value is of int type because it is our row number So I made a node like this Now like set, what I was doing in set Internally I was taking an array Let's say 5 size. So 0, 1, 2, 3, 4. I took an array of 5 size. Now I will work exactly as we did in the set. So suppose I have given a function add here which will take two things. One will take name and one will take roll number. So someone has to add name and roll number. And what will be my functionality from inside? Nothing. First of all, I will make an object of my node class. So I will write node n1 equal to let's say new node. Now assume that I have not shown in my node class but there is a constructor which gives key comma value in its bracket. So what did I do? I passed name in the form of key and roll number in the form of value. So if we do n1. key then name will come and if we do n1. roll number then this roll number will come. Now what I can do is, I have a node. Now what I will do is, I will hash the key which is name. So I will remove the hash code. Name, because I always want to hash the key. The special thing about map is that the key can never be duplicated. So it means that I can't have another person in my use case in the name of Aditya. Although we took it a bit the other way around, because generally the roll number is unique and the name can be the same. But okay, if we want to take an example of this, then let's take this. Now I will assume that no one else's name can be Aditya. And even if it is, then I will say that this is duplicate. So whose hash code will I take out? I will take out the name, because this cannot be duplicated. So I will get a hash code of name, I will get an integer value, I will do that mod Again with the size of array which is 5 So I will get a value between 0 and 4 Now whatever value will come in that value I will insert this entire node So let's say I get a value of 2 So I put my n1 on the second index Something like this Now you must have understood that let's say a new person will come to me Rohit, his roll number is 102 And he will call this add method So what will happen? A node will be created from inside Equal to new node And there we will pass its value In Rohit and 102 constructor and I will take the hashcode of name and modulo it to 5 so I will have a value from 0 to 4 and I will put this new node in that value so let's say I have a value of 4 so I will.

  234. Speaker 2

    Put my n2 here.

  235. Speaker 1

    Doing is that what I was doing in the set In my array, I am adding only one element in a particular index But in the case of set, I only had values So let's say it's just a string or numbers or something But it will be just a value So I would only hash it and add it to the set But in the case of maps, I have a key and a value. So a key value is a pair. So for this pair, I made a node class. You can also name it a pair. But again, generally, we name it a node. So it's a key and a value. Now I said, what can't be duplicated? Can't a key be duplicated? So I'll take out the hash code of the key and modulate.

  236. Speaker 2

    It with 5.

  237. Speaker 1

    So this is how my add method works. Okay, now my second method can be one more, which is get. In which I said, I'll give you a key. So let's say I gave get Aditya. So I asked you get Aditya. And you have to give me its roll number in constant time. So Aditya's roll number is 101. So you will return me 101 in constant time. Okay.

  238. Speaker 2

    So again how can I do this? Now this is also very simple. You have given me key.

  239. Speaker 1

    So again what I will do is whatever key you have given me, I will remove its hash code. Hash code of key. And I will modulo it from size of the array which is 5. I will get an index 0 to 4. Now you think if this key is Aditya. And I had inserted Aditya here, on the second index. So even when I had inserted it here, I had taken out the hash of its name. And you know, two strings whose value is the same, the hash code of its name will also be the same. So again, what will be the hash code of Aditya here?

  240. Speaker 2

    It will be 2.

  241. Speaker 1

    I will go to 2, I will see that I have node n1. I will return that n1.

  242. Speaker 2

    Value. Simple. Okay?

  243. Speaker 1

    So I am doing the same thing in the map that I was doing in the set. But the problem is that I was storing one value in the map, and I am storing a pair in the set.

  244. Speaker 2

    Key comma value. And how do I reach the key?

  245. Speaker 1

    Again, I take out the hash code of my key, so I write key modulo, which is the size of my array, 5. So I get an element from 0 to 4, and whatever I get, I insert that whole node in it. Something like this. So, haven't all my requirements been fulfilled? I searched in constant time, and didn't let duplicate element come. So, this is my whole map, which is a concept of our DSA.

  246. Speaker 2

    Again, do we need to make it? The answer is no.

  247. Speaker 1

    Java provides us with a hash map which does the same thing. We have another one, a linked hash map. Again, we will understand the difference between these two when we study it deeply. But now you understand that Java already has all these things. Java has made classes. Now let's move on to our next data structure and discuss what is a tree data structure. Suppose we have data in hierarchical form. For example, specifically we have BST in data structure which we call binary search tree. Binary search tree stores information in a hierarchical form. For example, if I have a number 10, it will store it like this. If you want to insert a number 5, it will come to the left. If you want to insert a number 16, it will come to the right. You insert data in the form of a tree And the number of nodes of that tree depends on whether it is a binary tree or a ternary tree Something like this So here our BST also means binary tree Which means there can be two children of one node Like we have two children of 10 One is 5 and one is 16 Something like this Now.

  248. Speaker 2

    Where does this work?

  249. Speaker 1

    Now you think we have to model a data Which can be expressed in the form of a tree Now a very good example of this is File and folder Think about how files and folders are stored in your operating system. First of all, you make a folder. Let's say I call the folder like this. You can keep multiple files in it. But you can also keep multiple folders in a folder. So let's say you put one more folder inside one folder. And put a file outside here. In this folder, you can again keep a file and keep another file. So, how is your hierarchy looking? It doesn't look like a tree. Now, if you want, you can keep a folder here and multiple files in it. So, your data is being expressed in the form of a tree. Now, here, there are only two childs of one folder. One folder and one file. But, it can be that there is a third and fourth file. Right? There can be one more file. There can be one more folder. It can have its own hierarchy. This can also happen.

  250. Speaker 2

    Okay?

  251. Speaker 1

    But, what I mean to say is that any such example in which you will need to keep hierarchical data. In that you will use whose tree it is And generally we have the most interesting tree which is binary search tree Which says that data should be stored like this Let's say if you want to enter 10 then you will store 10 at root level Now if you want a smaller number than 10 Then you will get it on the left side of 10 And if you want a bigger number than 10 then you will get it on the right side of 10 Now left side itself is your tree So suppose here the value is 5 and in the left of 5, it will be a smaller number and in the right of 5, it will be a bigger number. The same value is here, let's say 16. In the left of 16, it will be a smaller number but the smaller one should be bigger than 10 because the root is 10 and in the right of 16, it will be a bigger number than 16. So if you want to store this kind of data, You use tree data structure and here we are talking about binary search tree in which there can be only two child of one node.

  252. Speaker 2

    Can you make it by yourself? The answer is absolutely yes.

  253. Speaker 1

    So as we had made a class of our node, we can also make a tree node here.

  254. Speaker 2

    Tree node class. And we will keep three things in it. One will be our data.

  255. Speaker 1

    Every node has a data, so let's assume that in heap memory, if we have a node, it should have a data like 10. Now, in the link list, we had a reference pointer that points to the next object. But in the case of tree, one object is pointing to two objects. Like one folder is pointing to another folder or another file. So in that case, I'll have to keep two reference pointers of tree node type. So I'll keep one reference pointer of tree node, which I'll name left. And I'll keep one more tree node. Reference pointer which I will name as right. Now what is left right? It tells us that there are two reference variables. Why did I name it left right? Because it is a naming convention. Because I know that I am storing one node in left and one node in right. And again all this is conceptual. Actually how will our data be stored in heap? You know that this will be our heap memory. And one will be our stack memory. Now you know that in the upper class I have called tree node n1 equal to new tree node and let's say I wrote n1. data equal to 10 and you know that n1.

  256. Speaker 2

    Left and n1.

  257. Speaker 1

    Right will be null by default then some concept will be like this that I will make a node in which data will be 10 and its name will be n1 and it will point like this now if I have to represent this entire hierarchy then you can understand what will happen let's say I will make a new node let's say 12 and I will make a new node let's say 6 so I have to represent it 10 6 on the left side because it is smaller and 12 on the right side So how will I do it? I will make a new node 12, let's say n2 I will make a new node 6, let's say n3 And I would have made it in the same way by making everything independent Then I can write something like this, n1. left, n1' s left pointer or left reference variable Put n3 in n1' s left, n3 So one point is done and I will write again.

  258. Speaker 2

    N1.

  259. Speaker 1

    Write, which is the right reference variable, put n2 in it.

  260. Speaker 2

    So this is one point.

  261. Speaker 1

    So what I mean to say is that the data I represented conceptually will be represented like this in the memory. So I can make this too. I can make a tree node of my own. Again, it can take a static internal class. And I can provide some methods like add, remove, Contains, something like this. Add will add an element, remove will remove an element. Contains will check if an element exists or not. And in this way, what can I implement? Binary search tree. Specifically binary search tree. Trees are of many types, I am talking about BST only. And if I want BST, do I need to do all this?

  262. Speaker 2

    Answer is no.

  263. Speaker 1

    Java has given me two things. One is class TreeSet and the other is class TreeMap. Java has given us these two classes which use our BST from inside. Actually, it is a self-balancing binary search tree which we will start. We will read about this in detail when we understand how our data is stored in tree set and tree map in BST form, what is happening from inside, etc. But I hope you will get a good overview that you also have its classes. You can use it like abstract classes. You don't need to know how it works from inside. Now think, I told you that Java has given you all the things So Java has given you an ArrayList class And Java must have defined some methods in that class That you can add, remove, etc.

  264. Speaker 2

    In the ArrayList.

  265. Speaker 1

    Similarly, Java must have given you a class called LinkList And it must have defined some methods in that too That you can add, remove, etc.

  266. Speaker 2

    In that too.

  267. Speaker 1

    Right? And in the same way, Java would have done it with set, right? Or it would have done it with our queue, etc. So there would be a lot of classes. But you think, if Java would have made such independent classes, then there would be different methods in those classes. And maybe their names would be different. We would have to remember so many methods, we would have to remember so many classes. It would have been very complicated. So Java thought, because.

  268. Speaker 2

    What are all these?

  269. Speaker 1

    By combining them, they make a data structure. Data structure. And what does data structure mean? To store data in a container. These are all containers. Only the way of container has changed. So assume that all these are containers. Array stores in a different way. Link list stores in a different way. And setor might store in a different way. But actually, what are all of them doing? They are working to store data. So can't I create a hierarchy for all of them? And if I create a hierarchy for all of them, then what benefit do I get?

  270. Speaker 2

    I get the benefit that now I can use inheritance and polymorphism.

  271. Speaker 1

    And what is the benefit of using both of them?

  272. Speaker 2

    You know, I can reuse methods.

  273. Speaker 1

    So the method that came into my parent class once, I don't need to put it in the child class again. What does it mean to say? It's very simple. Java said, even if there is an array list, in which we store data in array, Whether it is your linked list, in which we store data in linked list. We can store data in any way. How are you storing data? Are you doing it in the form of a list? In array, the list is just contiguous, 2, 3, 4, 5, something like that.

  274. Speaker 2

    In linked list, they are different. After 2, 3, after 3, 4, and after 4, 5.

  275. Speaker 1

    They are just stored in distant memory, and these are stored in contiguous memory. But actually, this data is stored in the form of a list. So, Java thought, what if I make a parent interface of these two? whose name will be list because array list and linked list both are of list type so I made a parent interface of list array list and linked list both will implement this interface now the benefit of this will be that I know what is the meaning of list in list I can add element in list I can remove element in list in list I can replace element in list or I can delete anything I.

  276. Speaker 2

    Will be able to do it in both the lists.

  277. Speaker 1

    I don't need to give the same method in the array list and the link list. So now because I have declared all these methods in the list and the list is an interface. So if I want, I can write it here that this is my interface. So because if the list has declared all the methods as an interface, then you know that our array list and our link list will work together to provide the definition of all these methods.

  278. Speaker 2

    That is, our add function came automatically in this too. and also in our linked list.

  279. Speaker 1

    And both will provide its implementation in their own way. You know how the add method of ArrayList works, we had seen it. It will store the value in the next index available. And the add method of linked list will work in a different way. It will first make a node, store the value in it, and will point the old node to the new node.

  280. Speaker 2

    Right?

  281. Speaker 1

    So, the add method of both will work in different ways.

  282. Speaker 2

    But there is add in both.

  283. Speaker 1

    Right? And this is the special thing about polymorphism. both the add methods will be executed in their own way and any user who is using the list doesn't care whether the array list is being used from inside or the linked list is being used. He will simply call the add method on his list. So basically I got a very good abstraction that I can declare in my code.

  284. Speaker 2

    That I will write a list.

  285. Speaker 1

    Now because they use generics, so I can write like this, list of integer l equal to new. Now here, whether I use array list or if I want to use my link list, I will get the same functionality. Now on this L type variable, which is of the list type, if I call l.add and pass the answer, let's say 4, my 4 will be added to a list. Now I don't know, from inside Java is working on a normal array, which I call ArrayList, or is working on a link list, which I call LinkList. I don't need to know, I will just call those methods on my L. And the good thing about this is that the name of all my methods will always be the same. Add is in both, remove, replace, delete. Insert, I will get everything in both. So this is the beauty of polymorphism. And Java uses polymorphism very well. Interestingly, Java thought, why hierarchy only in list? Our list interface, or you consider a set, or you consider a queue, All of these are a form of data storage. So Java thought, what if I make a parent interface of all of these. And I name that interface collection. Because actually these are collections. They are a way of storing things. So Java named it collection and said that all of these are a collection. Now this is also an interface. This is also an interface. And these three are also interfaces. In fact, there are multiple classes in the list. One is the array list, the other is the link list, and there are more that we will discuss in detail. Similarly, there is a lot of hierarchy in the set and there is further hierarchy in the queue. But Java has called all of this collection. And this is our collection framework. All the data structures are in the same umbrella. If I show you a complete hierarchy in our collection framework in java then it will look something like this. So this is our complete hierarchy. At the top is the object class, you know this is the parent of all class. Below that is the iterable, below that is the collection. And we have different types of collections. List collection, queue, and set. Now again, they have further hierarchies. Like we can have array list, link list. Here we have other things like vector, stack. We will read that. And queue also has many classes. There is an array deck, a priority queue, etc. And we also have hash set, linked hash set and.

  286. Speaker 2

    Tree set in the set. There are many ways.

  287. Speaker 1

    But the way to store all these data, which we call data structures, lies in the same umbrella, which we call the collection of Java. And here polymorphism is used. Because I will declare some methods here. Collection is my own interface. So let's say I declared a method in this interface.

  288. Speaker 2

    Let's say add.

  289. Speaker 1

    So you can understand. Now this add will come to this list as well. And this will come to Q as well. And this will come to set as well. And the same hierarchy in the queue and set of this list, all of them will have to implement the add method. That means if I think, then I can add an element in every data structure. The answer is yes. So whatever I can add, I put it in the collection. Right? Why should I keep them in specific classes? So this is the benefit of the entire collection framework. Now, what is itrable? What are the methods in collections? And what is their hierarchy? We will discuss that gradually. But here you observe one thing. I have made the map out of this hierarchy. And it is also true that the collection framework of Java is different and the framework of the map is different, although we read both of them together. And what is the reason for this? We will understand it in detail. But you can understand it in detail because the map deals with key comma value 2 and this entire collection framework deals with our values only. That's why Java has kept them separately, in a separate hierarchy. Here in the map, there is a further hierarchy, like here as well, HashMap, LinkedHashMap, tree map, etc. We will discuss their hierarchy. But this is our collection framework. Now we have understood its hierarchy. We have understood the intro of every data structure. But now we will go deep inside and see how list, queue, and set are implemented in Java. What are the methods in it? And how do we use them? And again we will see our collection framework and iterable well. That's all for this video. We will meet you in the next video. Till then, thank you so much.