- 10 hours ago
Transcript
00:00Now let's talk about how we can iterate on the set of elements. There are two ways in the sets.
00:06One we use iterators and the other way we use enhanced for loop. Enhanced for loop is basically an advanced
00:14for loop which we are writing on the hash map. First let's talk about iterators. There is an interface in
00:24Java.
00:26This interface use and we can iterate on the sets. If I call a set for .iterator function, what will
00:35I do? It will return an iterator. For int i equals 0, i less than array dot length, i plus
00:47plus.
00:48So, in this loop, what I was saying? I was calling my iterator. Iterator is a pointer for a loop
00:56on my data structure or traversing it. So, in this way, sets are already enabled iterators. When I call the
01:07set method of iterator, I return my iterator. And with this iterator help, I can use my set elements.
01:16So, as I have written a set.iterator, what will I do? I will return an iterator. I can store
01:22that iterator in an object in the iterator type. So, this is my iterator type of object, which I have
01:30given the name, it. You can also give it a name, itr, itr, itr, itr, itr, itr, itr, itr, itr,
01:36itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr,
01:36itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr, itr,
01:37itr, itr, itr, it
01:46It.hasNext. HasNext is a function. Iterator track that something exists before it exists. And then every time we print
01:59it to next. This code will be a little interesting to see how iterators are used in Java. I will
02:11take an example of collection. Let's say my set is collection. This is my set. It is some elements inside.
02:19Remember the elements first you have one, two, eight, five, nine, twelve. Now let's say this is stored in an
02:29unordered way.
02:30store first, remember 1, then 12, then 5, then 2, then 8, then 9. What is IT? IT is an
02:39iterator, which can point first a null value. This value can be null, dummy, garbage value. At the start of
02:46the value, this is not my concern. My concern is that the iterator, the set of elements, how do I
02:53traverse them?
02:55So, in the beginning, it is pointing to a random value. So, I will check what it is next. What
03:02is hasNext function? This is my hasNext function. This function tells me that there is an element that exists from
03:09it that is my set part which I have not yet traversed. So, it will see it, it will see
03:15one.
03:16So, he will say yes. So, in this case, it will return true. So, we will print what it will
03:20be further. What it will be further? What it will be further? What it will be further? It will have
03:24one value. So, I will print one value. After that, automatically, it.next function goes to next. So, it will
03:33be updated here and it will come to this value.
03:36Now I will ask again if it has its next? It has its next which is 12. So what will
03:42it do? It will return its next. If it has its next return as 12, then I have printed it
03:47and it will automatically update. Then I will ask again if it has its next. It will be 5. So
03:57I will print it next. It will be 5 and it will automatically update to this value.
04:035. After that, I will ask again what it has next. Next is which is equal to 2. So, I
04:09will print it from it.next to 2. As 2 this value will be printed, it will be updated by
04:152. Then, I will ask again what it has next. Next is which is 8. So, I will print it
04:22from it.next to 8. My 8 value will be printed and then my it will be updated to this
04:29value.
04:30After that, I will ask my loop again if it has next. The next value is that is equal to
04:369. So, what will it do? It will print 9 to it.next and then it will update itself to
04:43this element 9. After that, what will it do? What will it do? It will ask if it is next.
04:49In this case, there will be false return because it will be next. So, we will come back from our
04:54loop.
04:54Now, as I have put this loop, what will happen? My set of all elements will be printed on my
05:00screen. How much time will it? Big of end time. So, we work with iterators in hash sets. Once we
05:08implement this iterator, what will I do? I will create a hash set where I will store some strings. I
05:15will name the strings. What will I do for cities? I defined a new hash set.
05:21First of all, I will add any city. I have added Delhi. Then, I have added cities in Mumbai. Then,
05:34I have added cities in Noida. Then, I have added cities in Bangalore. When I have added cities, I want
05:46to traverse many cities. For traversing, I will make an iterator.
05:54is equal to my set or cities.iterator. So every set has a method that returns a valid iterator. It
06:06has its next values. So I will run a loop while my it has next. In that case, I will
06:14print it.next. So what will happen automatically? Its next value will also be printed and it will automatically be
06:21updated. Both steps will perform in this function.
06:25Save and Run.
06:29So I have 4 cities. First Delhi, then Bangalore, then Noida, then Mumbai. And you have seen that this is
06:35not in order. It means it doesn't mean that if you first Delhi came, then Delhi came. It could be
06:39Mumbai or Bangalore, no city. After Mumbai I added, so Mumbai is not printed, I have Bangalore printed. Then Noida
06:48is printed and Mumbai is printed. So my hash sets work in unordered ways. So this is my first method
06:55which I use iterators. My second method is Advances.
06:58advanced loop. Let us comment out this one. Advanced loops we have already done. How do we do it? I
07:04can write for which I have defined in my hash set. I will write string s or city in my
07:14cities.
07:17So, what did I do? I wrote my hash-sit name on the right side, which is my collection, which
07:22I'm talking about. And I put my elements in the collection. So, what is stored in cities? It is stored
07:29in cities. So, if I write here for string city, colon, cities, then I put all the cities in this
07:36string. And then I can print them in this for loop. So, I printed every city. Let's do this one,
07:44save and run.
07:47How to print cities? I have Delhi, then Bangalore, then Noida, then Mumbai. So either I can use the iterator
07:54or not, I can put an enhanced or advanced for loop or for each loop. We can say it every
08:02time I can print the city's value and I can do something with that value. I can print it, I
08:07can perform operations, I can remove the longest city's name, I can arrange it alphabetically, maximum or smallest.
08:16So many operations we perform with looping and iterators.