java-development-for-beginners-learnit / 19 - Java Collections Framework /010 Iterable, Iterator & ListIterator_en.srt
| 1 | |
| 00:00:06,000 --> 00:00:06,000 | |
| Hello, Jim. | |
| 2 | |
| 00:00:06,000 --> 00:00:11,000 | |
| In this lesson, we are going to talk about what iterable and iterator interfaces are. | |
| 3 | |
| 00:00:11,000 --> 00:00:17,000 | |
| We would investigate source code of these two interfaces and also will answer what is the role of these | |
| 4 | |
| 00:00:17,000 --> 00:00:19,000 | |
| two interfaces in Java collections framework. | |
| 5 | |
| 00:00:20,000 --> 00:00:23,000 | |
| After this lesson, you will never confuse these two interfaces. | |
| 6 | |
| 00:00:23,000 --> 00:00:26,000 | |
| We are going to learn three types of iterator. | |
| 7 | |
| 00:00:26,000 --> 00:00:32,000 | |
| And at the end of the lesson, I also want to demo what least Iterator is and how it is different from | |
| 8 | |
| 00:00:32,000 --> 00:00:33,000 | |
| Iterator. | |
| 9 | |
| 00:00:33,000 --> 00:00:34,000 | |
| Let's start. | |
| 10 | |
| 00:00:34,000 --> 00:00:40,000 | |
| I believe you remember that first time when we looked at collection interface, we found that collection | |
| 11 | |
| 00:00:40,000 --> 00:00:43,000 | |
| interface is extended from iterable interface. | |
| 12 | |
| 00:00:43,000 --> 00:00:50,000 | |
| You remember that as you can see on this class diagram, iterable interface has only one method iterator. | |
| 13 | |
| 00:00:51,000 --> 00:00:53,000 | |
| This method should return object of iterate type. | |
| 14 | |
| 00:00:54,000 --> 00:00:59,000 | |
| That means that each implementation of collection interfacing Java should have iterator. | |
| 15 | |
| 00:00:59,000 --> 00:01:03,000 | |
| I believe that is all what we can say about iterable interface. | |
| 16 | |
| 00:01:03,000 --> 00:01:10,000 | |
| This interface is to tell that I'm the type that has iterator more interesting to talk about what an | |
| 17 | |
| 00:01:10,000 --> 00:01:17,000 | |
| iterator is and to answer this question, I suggest reviewing source code of iterator type in eclipse | |
| 18 | |
| 00:01:17,000 --> 00:01:18,000 | |
| in few words. | |
| 19 | |
| 00:01:18,000 --> 00:01:22,000 | |
| Iterator is a type that can go over each element in a container. | |
| 20 | |
| 00:01:22,000 --> 00:01:23,000 | |
| That's it. | |
| 21 | |
| 00:01:23,000 --> 00:01:27,000 | |
| You might be wondering, is it really so hard and separate type is needed? | |
| 22 | |
| 00:01:28,000 --> 00:01:31,000 | |
| It is not so hard, but imagine two simple cases. | |
| 23 | |
| 00:01:31,000 --> 00:01:34,000 | |
| You have array lists and linked list. | |
| 24 | |
| 00:01:34,000 --> 00:01:39,000 | |
| In our list we are together elements from the array by the indexes in the linked list. | |
| 25 | |
| 00:01:39,000 --> 00:01:44,000 | |
| We have to get the reference to the next node from each node to iterate over elements. | |
| 26 | |
| 00:01:44,000 --> 00:01:50,000 | |
| Can you see that the way how to iterate over each element, even in these two collections, is completely | |
| 27 | |
| 00:01:50,000 --> 00:01:51,000 | |
| different? | |
| 28 | |
| 00:01:52,000 --> 00:01:58,000 | |
| That's exactly the reason why we need to come up with some abstract type like Iterator to set the interface | |
| 29 | |
| 00:01:58,000 --> 00:02:02,000 | |
| that should be implemented by specific implementations of collection interface. | |
| 30 | |
| 00:02:02,000 --> 00:02:05,000 | |
| So let's sum up what Iterator is. | |
| 31 | |
| 00:02:06,000 --> 00:02:12,000 | |
| Iterator is an interface that allows access to the elements in container and navigation in collection, | |
| 32 | |
| 00:02:12,000 --> 00:02:19,000 | |
| including iteration or in other words, looping over each element in any collection without knowing | |
| 33 | |
| 00:02:19,000 --> 00:02:22,000 | |
| the details of implementation of specific collection. | |
| 34 | |
| 00:02:23,000 --> 00:02:30,000 | |
| Now we are going to run the interface of iterate the type it has to abstract and two default methods. | |
| 35 | |
| 00:02:31,000 --> 00:02:33,000 | |
| Let me explain what each of these methods is about. | |
| 36 | |
| 00:02:34,000 --> 00:02:37,000 | |
| Has an X method returns true or false? | |
| 37 | |
| 00:02:37,000 --> 00:02:43,000 | |
| It tells us whether the next element is present will use this method in loops to check if it was to | |
| 38 | |
| 00:02:43,000 --> 00:02:49,000 | |
| go on with our loop or we reached the end of the collection and we have to break the loop. | |
| 39 | |
| 00:02:50,000 --> 00:02:53,000 | |
| Next method returns the next element in connection. | |
| 40 | |
| 00:02:53,000 --> 00:02:54,000 | |
| Be careful. | |
| 41 | |
| 00:02:54,000 --> 00:03:01,000 | |
| This method may throw no such element exception and interrupt your program execution in case you called | |
| 42 | |
| 00:03:01,000 --> 00:03:02,000 | |
| next method. | |
| 43 | |
| 00:03:02,000 --> 00:03:04,000 | |
| But there are no any elements there. | |
| 44 | |
| 00:03:04,000 --> 00:03:07,000 | |
| That is a contract of the Iterator interface. | |
| 45 | |
| 00:03:07,000 --> 00:03:09,000 | |
| We can do nothing about that. | |
| 46 | |
| 00:03:09,000 --> 00:03:16,000 | |
| That's why before calling next method, we have to always call has next method remove method supposed | |
| 47 | |
| 00:03:16,000 --> 00:03:21,000 | |
| to remove the last element returned by next method and now attention. | |
| 48 | |
| 00:03:21,000 --> 00:03:25,000 | |
| This method can be called only once after each next method call. | |
| 49 | |
| 00:03:25,000 --> 00:03:31,000 | |
| We can't call two times next method and after that call two times remove method. | |
| 50 | |
| 00:03:31,000 --> 00:03:37,000 | |
| That is the way how Iterator should work in case you would call remove method without calling next method | |
| 51 | |
| 00:03:37,000 --> 00:03:42,000 | |
| or we'll call remove two times in a row in your program, your program will be stopped with illegal | |
| 52 | |
| 00:03:42,000 --> 00:03:43,000 | |
| state exception. | |
| 53 | |
| 00:03:44,000 --> 00:03:46,000 | |
| But there is more interesting thing here. | |
| 54 | |
| 00:03:46,000 --> 00:03:50,000 | |
| By default, remove method throws and support the separation exception. | |
| 55 | |
| 00:03:50,000 --> 00:03:56,000 | |
| That means that in case we would call this method, our program will be stopped with the exceptional | |
| 56 | |
| 00:03:56,000 --> 00:03:56,000 | |
| situation. | |
| 57 | |
| 00:03:57,000 --> 00:04:01,000 | |
| Now I'm going to say not only my opinion, but opinion of different engineers too. | |
| 58 | |
| 00:04:02,000 --> 00:04:06,000 | |
| It is senseless to declare method in the interface that is not needed. | |
| 59 | |
| 00:04:06,000 --> 00:04:07,000 | |
| Yes, exactly. | |
| 60 | |
| 00:04:08,000 --> 00:04:14,000 | |
| This exception here and support the separation exception tells us that by default this method is not | |
| 61 | |
| 00:04:14,000 --> 00:04:15,000 | |
| supported. | |
| 62 | |
| 00:04:15,000 --> 00:04:18,000 | |
| Don't do that during the implementation of your interfaces. | |
| 63 | |
| 00:04:19,000 --> 00:04:19,000 | |
| Why? | |
| 64 | |
| 00:04:20,000 --> 00:04:21,000 | |
| Let me explain here. | |
| 65 | |
| 00:04:21,000 --> 00:04:24,000 | |
| We can see a violation of one of the solid principles. | |
| 66 | |
| 00:04:25,000 --> 00:04:29,000 | |
| Can you guess what solid principle is violated in the Iterator interface? | |
| 67 | |
| 00:04:30,000 --> 00:04:31,000 | |
| Exactly. | |
| 68 | |
| 00:04:31,000 --> 00:04:34,000 | |
| High principle interface aggregation principle. | |
| 69 | |
| 00:04:34,000 --> 00:04:39,000 | |
| According to this principle, no client should be forced to depend on methods it doesn't use. | |
| 70 | |
| 00:04:40,000 --> 00:04:46,000 | |
| Originally, according to the documentation, Iterator type was created to iterate over elements, but | |
| 71 | |
| 00:04:46,000 --> 00:04:47,000 | |
| not to remove them. | |
| 72 | |
| 00:04:47,000 --> 00:04:50,000 | |
| How would I recommend implementing this? | |
| 73 | |
| 00:04:50,000 --> 00:04:56,000 | |
| I would create something like removable iterator that extends iterate type and would let client of my | |
| 74 | |
| 00:04:56,000 --> 00:05:03,000 | |
| code to decide whether they need to remove method or no, because right now each implementation of Iterator. | |
| 75 | |
| 00:05:03,000 --> 00:05:10,000 | |
| Interface has removed MassArt, even if it is not a that this is default MassArt and there is no need | |
| 76 | |
| 00:05:10,000 --> 00:05:10,000 | |
| to override it. | |
| 77 | |
| 00:05:11,000 --> 00:05:18,000 | |
| But this does not make life of engineers easier because we still can call, remove by mistake and rely | |
| 78 | |
| 00:05:18,000 --> 00:05:21,000 | |
| on the fact that this behavior should be supported in iterator. | |
| 79 | |
| 00:05:22,000 --> 00:05:24,000 | |
| Now, you know that there is an exception here. | |
| 80 | |
| 00:05:25,000 --> 00:05:30,000 | |
| The other side of this violation is that in the case in Java collections framework, engineers would | |
| 81 | |
| 00:05:30,000 --> 00:05:35,000 | |
| follow all or P principles, including interface aggregation principle. | |
| 82 | |
| 00:05:35,000 --> 00:05:42,000 | |
| There would be big bank of interfaces that is hard to learn and hard to remember and even harder to | |
| 83 | |
| 00:05:42,000 --> 00:05:42,000 | |
| maintain. | |
| 84 | |
| 00:05:43,000 --> 00:05:49,000 | |
| So as you can see, programming like everything else in our lives is all about tradeoffs and compromises. | |
| 85 | |
| 00:05:50,000 --> 00:05:56,000 | |
| The last but not the least massaged here in Interactor interface is for each remaining, this matter | |
| 86 | |
| 00:05:56,000 --> 00:06:01,000 | |
| takes function that should be applied for each element that remains in the container. | |
| 87 | |
| 00:06:01,000 --> 00:06:07,000 | |
| The usage of this message requires knowledge of functional programming in Java and no interfaces from | |
| 88 | |
| 00:06:07,000 --> 00:06:09,000 | |
| the Java Wattyl function package. | |
| 89 | |
| 00:06:10,000 --> 00:06:13,000 | |
| That's why today will not Damul work of this method. | |
| 90 | |
| 00:06:13,000 --> 00:06:16,000 | |
| But we will damage it in the lesson about functional programming. | |
| 91 | |
| 00:06:17,000 --> 00:06:23,000 | |
| OK, now when we know iterate the interface, I suggest learning how it works and to practice a bit. | |
| 92 | |
| 00:06:24,000 --> 00:06:28,000 | |
| And I would like to visualize the way how Massaponax and remove works. | |
| 93 | |
| 00:06:28,000 --> 00:06:30,000 | |
| Let's take a look at this slide. | |
| 94 | |
| 00:06:30,000 --> 00:06:36,000 | |
| I want you to imagine that Iterator works like a cursor, namely at the beginning. | |
| 95 | |
| 00:06:36,000 --> 00:06:38,000 | |
| It stands right before the first element. | |
| 96 | |
| 00:06:39,000 --> 00:06:43,000 | |
| When we call next method, it returns the next element to us. | |
| 97 | |
| 00:06:43,000 --> 00:06:49,000 | |
| And now cursor stands between the first and the second element in case we call next method one more | |
| 98 | |
| 00:06:49,000 --> 00:06:55,000 | |
| time cursor is moved to the position between second and the third element like you see on the slide. | |
| 99 | |
| 00:06:56,000 --> 00:07:02,000 | |
| Now, when we call remove method Iterator works as a backspace key on the Windows machines, it removes | |
| 100 | |
| 00:07:02,000 --> 00:07:05,000 | |
| elements that stands right before the cursor. | |
| 101 | |
| 00:07:05,000 --> 00:07:06,000 | |
| Does it make sense? | |
| 102 | |
| 00:07:07,000 --> 00:07:08,000 | |
| Now let's get back to Eclipse. | |
| 103 | |
| 00:07:09,000 --> 00:07:15,000 | |
| For the sake of the drama, I created this demo file to practice a bit with Iterator, let me run this | |
| 104 | |
| 00:07:15,000 --> 00:07:18,000 | |
| program and walk you through the different cases that are listed here. | |
| 105 | |
| 00:07:19,000 --> 00:07:24,000 | |
| At the beginning, I created the list of integers to play with, you already know this construction, | |
| 106 | |
| 00:07:25,000 --> 00:07:27,000 | |
| so there is nothing special for you here. | |
| 107 | |
| 00:07:27,000 --> 00:07:30,000 | |
| Now, let's iterate over each element in container. | |
| 108 | |
| 00:07:31,000 --> 00:07:37,000 | |
| I call iterate a method on our list that returns iterator object parametrized by integer type. | |
| 109 | |
| 00:07:37,000 --> 00:07:44,000 | |
| This means that when I will call next Masset iterate, I will reach an object of type integer like it | |
| 110 | |
| 00:07:44,000 --> 00:07:49,000 | |
| specified here in Diamond Operator, but not the object of type object. | |
| 111 | |
| 00:07:49,000 --> 00:07:55,000 | |
| After that we have while loop and I check in condition statement here was an iterator has an extra element | |
| 112 | |
| 00:07:55,000 --> 00:07:56,000 | |
| or no. | |
| 113 | |
| 00:07:56,000 --> 00:08:03,000 | |
| And if yes I want to print it to cancel next match it returns me means a reference to the next element | |
| 114 | |
| 00:08:03,000 --> 00:08:04,000 | |
| in my container. | |
| 115 | |
| 00:08:04,000 --> 00:08:06,000 | |
| Is everything clear here. | |
| 116 | |
| 00:08:06,000 --> 00:08:10,000 | |
| Haubert is the next example is more like practical type. | |
| 117 | |
| 00:08:11,000 --> 00:08:17,000 | |
| Some of my students that I taught offline feel concerned that they can use the same iterator once we | |
| 118 | |
| 00:08:17,000 --> 00:08:19,000 | |
| iterate it over all elements. | |
| 119 | |
| 00:08:19,000 --> 00:08:26,000 | |
| So after the previous loop, our iterator cursor is at the end of the container and has an X method | |
| 120 | |
| 00:08:26,000 --> 00:08:27,000 | |
| will return false. | |
| 121 | |
| 00:08:27,000 --> 00:08:31,000 | |
| That is exactly the reason why we see this console output here. | |
| 122 | |
| 00:08:32,000 --> 00:08:33,000 | |
| But what to do now? | |
| 123 | |
| 00:08:33,000 --> 00:08:40,000 | |
| We can use the same variable and initialize it with a new iterator object because no message to reset | |
| 124 | |
| 00:08:40,000 --> 00:08:44,000 | |
| courser obliterated back again to the beginning like we did here. | |
| 125 | |
| 00:08:45,000 --> 00:08:46,000 | |
| So that is fine. | |
| 126 | |
| 00:08:47,000 --> 00:08:49,000 | |
| Now I want to them how remove method works. | |
| 127 | |
| 00:08:50,000 --> 00:08:56,000 | |
| Let's remove all even numbers from our container we call next method to get the element from the container. | |
| 128 | |
| 00:08:57,000 --> 00:09:02,000 | |
| After that, we check whether this number is A1 and if yes, then we remove it. | |
| 129 | |
| 00:09:02,000 --> 00:09:06,000 | |
| You can see that I don't violate the rules of remove method usage. | |
| 130 | |
| 00:09:07,000 --> 00:09:10,000 | |
| I call remove method only after I called next method. | |
| 131 | |
| 00:09:11,000 --> 00:09:14,000 | |
| And also there are no multiple locations of remove method. | |
| 132 | |
| 00:09:15,000 --> 00:09:22,000 | |
| You also can notice that iterator that is implemented in at least overwrites remove Masset and doesn't | |
| 133 | |
| 00:09:22,000 --> 00:09:27,000 | |
| throw and supports the separation exception like the full implementation of Remove Massud does. | |
| 134 | |
| 00:09:28,000 --> 00:09:33,000 | |
| Now, if you are not familiar with the exception topic yet, let me show you an example. | |
| 135 | |
| 00:09:33,000 --> 00:09:36,000 | |
| What exception means and how the writer may throw exceptions. | |
| 136 | |
| 00:09:37,000 --> 00:09:43,000 | |
| Lattman comments these lines of code I initialised again my iterative variable with the new iterator | |
| 137 | |
| 00:09:43,000 --> 00:09:44,000 | |
| object. | |
| 138 | |
| 00:09:44,000 --> 00:09:47,000 | |
| After that I call Remove Masset without calling. | |
| 139 | |
| 00:09:47,000 --> 00:09:48,000 | |
| Next Masset. | |
| 140 | |
| 00:09:48,000 --> 00:09:50,000 | |
| Let me run my program now. | |
| 141 | |
| 00:09:50,000 --> 00:09:53,000 | |
| And here we can see illegal state exception. | |
| 142 | |
| 00:09:53,000 --> 00:09:58,000 | |
| The same exceptions will be thrown in case we would call remove Massud two times in a row. | |
| 143 | |
| 00:09:59,000 --> 00:10:04,000 | |
| That is something what you have to remember at the end of the lesson I will share with your home task | |
| 144 | |
| 00:10:04,000 --> 00:10:09,000 | |
| where you will have opportunity to practice your skills in writing your custom iterators. | |
| 145 | |
| 00:10:09,000 --> 00:10:14,000 | |
| Let me comment these lines to be able to execute code that is written after this one. | |
| 146 | |
| 00:10:15,000 --> 00:10:21,000 | |
| Let me also show one more important exception that might be thrown by Iterator that is concurrent modification, | |
| 147 | |
| 00:10:21,000 --> 00:10:25,000 | |
| exception and uncommon these lines of code and will damage to you. | |
| 148 | |
| 00:10:26,000 --> 00:10:32,000 | |
| Imagine that we created Iterator and when we started iteration, we removed elements from the container, | |
| 149 | |
| 00:10:33,000 --> 00:10:37,000 | |
| but not with the help of Iterator, but just by using the removed method of our list. | |
| 150 | |
| 00:10:38,000 --> 00:10:39,000 | |
| This is just an example. | |
| 151 | |
| 00:10:39,000 --> 00:10:45,000 | |
| In real life, it might happen that you created an iterator on the list that is shared between different | |
| 152 | |
| 00:10:45,000 --> 00:10:50,000 | |
| threats of execution and somebody in another thread did some modifications to your list. | |
| 153 | |
| 00:10:51,000 --> 00:10:56,000 | |
| And after we would call next method, we would see concurrent modification exception. | |
| 154 | |
| 00:10:56,000 --> 00:11:01,000 | |
| That means somebody modified the collection after Iterator had been created. | |
| 155 | |
| 00:11:02,000 --> 00:11:07,000 | |
| Iterate an object, noticed that and want to let us know that collections that we are iterating over | |
| 156 | |
| 00:11:07,000 --> 00:11:09,000 | |
| is not up to date. | |
| 157 | |
| 00:11:09,000 --> 00:11:15,000 | |
| And that information about elements that I will read with the help of this iterator is not relevant | |
| 158 | |
| 00:11:15,000 --> 00:11:15,000 | |
| anymore. | |
| 159 | |
| 00:11:16,000 --> 00:11:19,000 | |
| This type of iterator is called fail fast iterator. | |
| 160 | |
| 00:11:20,000 --> 00:11:25,000 | |
| That is default implementation of iterators in majority of collection implementations that are not developed | |
| 161 | |
| 00:11:25,000 --> 00:11:27,000 | |
| to be used in multithreaded environment. | |
| 162 | |
| 00:11:28,000 --> 00:11:34,000 | |
| What to do in this case, in case you have iterate to make sure there is no other place in your code | |
| 163 | |
| 00:11:34,000 --> 00:11:36,000 | |
| where you modify the same collection simultaneously. | |
| 164 | |
| 00:11:37,000 --> 00:11:43,000 | |
| If you do modify collections from different sets of execution, then you need failsafe iterator. | |
| 165 | |
| 00:11:43,000 --> 00:11:47,000 | |
| That is a second type of iterator that usually called failsafe. | |
| 166 | |
| 00:11:48,000 --> 00:11:53,000 | |
| Let me Corman's the concurrent modification demo code to be able to proceed with the next one and I | |
| 167 | |
| 00:11:53,000 --> 00:11:55,000 | |
| ran the program again. | |
| 168 | |
| 00:11:55,000 --> 00:12:02,000 | |
| Usually thread safe implementation of collections also implements failsafe iterator to explain in short | |
| 169 | |
| 00:12:02,000 --> 00:12:04,000 | |
| how it works without going deep into the details. | |
| 170 | |
| 00:12:05,000 --> 00:12:11,000 | |
| Failsafe Iterate that makes a copy of the internal data structure and iterate over the corporate data | |
| 171 | |
| 00:12:11,000 --> 00:12:11,000 | |
| structure. | |
| 172 | |
| 00:12:12,000 --> 00:12:17,000 | |
| And a structural modification done to the Iterator doesn't affect the copied data structure. | |
| 173 | |
| 00:12:17,000 --> 00:12:20,000 | |
| So original data structure is changed. | |
| 174 | |
| 00:12:21,000 --> 00:12:26,000 | |
| This type of iterator creates a snapshot and iterate over elements in this snapshot. | |
| 175 | |
| 00:12:27,000 --> 00:12:30,000 | |
| The drawback of such iterator type is obvious. | |
| 176 | |
| 00:12:30,000 --> 00:12:36,000 | |
| In some cases, you may not receive latest and up to date version of elements in case collection is | |
| 177 | |
| 00:12:36,000 --> 00:12:38,000 | |
| modified from the different threats. | |
| 178 | |
| 00:12:39,000 --> 00:12:41,000 | |
| But sometimes it might be not critical for you. | |
| 179 | |
| 00:12:42,000 --> 00:12:48,000 | |
| Here we have thread safe list implementation copy and write a list I initialize. | |
| 180 | |
| 00:12:48,000 --> 00:12:50,000 | |
| It was elements that are in our integers list. | |
| 181 | |
| 00:12:51,000 --> 00:12:55,000 | |
| I create iterator interface of the that is the same everywhere. | |
| 182 | |
| 00:12:56,000 --> 00:12:58,000 | |
| That's why I use the same variables that I had created. | |
| 183 | |
| 00:12:59,000 --> 00:13:05,000 | |
| As I already mentioned, failsafe iterators creates a snapshot of the container to prove this. | |
| 184 | |
| 00:13:05,000 --> 00:13:13,000 | |
| Here I add another integer to this container and now when I iterate over elements and as in previous | |
| 185 | |
| 00:13:13,000 --> 00:13:15,000 | |
| example, I remove elements from collection. | |
| 186 | |
| 00:13:15,000 --> 00:13:19,000 | |
| Without Iterator, I still bring elements from the container to the console. | |
| 187 | |
| 00:13:20,000 --> 00:13:23,000 | |
| But why not print the collection to console? | |
| 188 | |
| 00:13:23,000 --> 00:13:27,000 | |
| It has only one number that we added after iterator creation. | |
| 189 | |
| 00:13:27,000 --> 00:13:33,000 | |
| This proves that Iterator creates a snapshot with elements, and this snapshot is not updated as we | |
| 190 | |
| 00:13:33,000 --> 00:13:36,000 | |
| update collection after creation of Iterator. | |
| 191 | |
| 00:13:37,000 --> 00:13:41,000 | |
| There is also a third type of iterator that is called weakly consistent. | |
| 192 | |
| 00:13:42,000 --> 00:13:48,000 | |
| It is called so because this type of iterator can reflect some, but not necessarily all of the changes | |
| 193 | |
| 00:13:48,000 --> 00:13:52,000 | |
| that have been made to the collection since Iterator is created. | |
| 194 | |
| 00:13:52,000 --> 00:13:53,000 | |
| Let me show you this. | |
| 195 | |
| 00:13:53,000 --> 00:14:00,000 | |
| I declare a new variable of collection type parametrized with integer type an aggregate object of concurrent | |
| 196 | |
| 00:14:00,000 --> 00:14:01,000 | |
| linked deck. | |
| 197 | |
| 00:14:01,000 --> 00:14:05,000 | |
| This collection implementation has weekly consistent Iterator implementation. | |
| 198 | |
| 00:14:06,000 --> 00:14:11,000 | |
| I create an iterator and now attention similar to the previous example. | |
| 199 | |
| 00:14:11,000 --> 00:14:14,000 | |
| I add new integer here and now. | |
| 200 | |
| 00:14:14,000 --> 00:14:20,000 | |
| When I'm iterating over each element, I can see that new integer already was taken into account here. | |
| 201 | |
| 00:14:21,000 --> 00:14:26,000 | |
| But as it is said in the documentation, there is no guarantee that all updates to the collection will | |
| 202 | |
| 00:14:26,000 --> 00:14:31,000 | |
| be taken into account by the moment Iterator would reach the specific element. | |
| 203 | |
| 00:14:31,000 --> 00:14:34,000 | |
| I understand now what is really consistent iterator. | |
| 204 | |
| 00:14:35,000 --> 00:14:39,000 | |
| So now we may say that we learned three types of the iterator. | |
| 205 | |
| 00:14:39,000 --> 00:14:44,000 | |
| The last thing that I would like to learn with you today is a list iterator. | |
| 206 | |
| 00:14:44,000 --> 00:14:46,000 | |
| Let's look at this interface. | |
| 207 | |
| 00:14:46,000 --> 00:14:51,000 | |
| The first thing that you can see is that list rate that extends iterate the interface. | |
| 208 | |
| 00:14:52,000 --> 00:14:58,000 | |
| The main feature that is introduced list, right, is to iterate over elements in the reverse direction. | |
| 209 | |
| 00:14:58,000 --> 00:15:04,000 | |
| If Iterator can go over elements from the beginning to the end, the list rate that can go from the | |
| 210 | |
| 00:15:04,000 --> 00:15:06,000 | |
| end to the beginning. | |
| 211 | |
| 00:15:06,000 --> 00:15:12,000 | |
| It also declares mass that absent in iterator interface, Zaya has Hesperus. | |
| 212 | |
| 00:15:12,000 --> 00:15:14,000 | |
| It is similar to his next. | |
| 213 | |
| 00:15:14,000 --> 00:15:21,000 | |
| But check if there any element in another direction, previs returns the reference to the previous element | |
| 214 | |
| 00:15:21,000 --> 00:15:29,000 | |
| in the container next index to get the index of the next element, previous index it returns. | |
| 215 | |
| 00:15:29,000 --> 00:15:36,000 | |
| The element that would be returned by previous MassArt set replaces the last element that was returned. | |
| 216 | |
| 00:15:36,000 --> 00:15:38,000 | |
| And this is important to understand. | |
| 217 | |
| 00:15:39,000 --> 00:15:46,000 | |
| It replaces elements that was returned after the next or previous method in case that method is called | |
| 218 | |
| 00:15:46,000 --> 00:15:51,000 | |
| without calling next the previous method before that illegal state exception strong. | |
| 219 | |
| 00:15:51,000 --> 00:15:59,000 | |
| Add method adds element to the container so you can see a few more important features of the list iterator. | |
| 220 | |
| 00:15:59,000 --> 00:16:05,000 | |
| It also allows us to add element to the container during the iteration over elements or to substitute | |
| 221 | |
| 00:16:05,000 --> 00:16:05,000 | |
| elements. | |
| 222 | |
| 00:16:06,000 --> 00:16:08,000 | |
| Let's look at the list, iterator them. | |
| 223 | |
| 00:16:08,000 --> 00:16:11,000 | |
| Now, I created a separate file for that. | |
| 224 | |
| 00:16:11,000 --> 00:16:13,000 | |
| Let me also execute this program. | |
| 225 | |
| 00:16:14,000 --> 00:16:17,000 | |
| I have the same container was integers as in previous example. | |
| 226 | |
| 00:16:17,000 --> 00:16:19,000 | |
| The list interface declares list. | |
| 227 | |
| 00:16:19,000 --> 00:16:20,000 | |
| Iterate the method. | |
| 228 | |
| 00:16:20,000 --> 00:16:25,000 | |
| As you remember, this method returns as the objects of list iterator type. | |
| 229 | |
| 00:16:26,000 --> 00:16:27,000 | |
| One more interesting feature. | |
| 230 | |
| 00:16:27,000 --> 00:16:31,000 | |
| We can get at least iterator from the specific position if we need. | |
| 231 | |
| 00:16:31,000 --> 00:16:36,000 | |
| I can't imagine the case when I would use it, but it is interesting feature to know about. | |
| 232 | |
| 00:16:36,000 --> 00:16:39,000 | |
| List Iterator, for example. | |
| 233 | |
| 00:16:39,000 --> 00:16:41,000 | |
| Here I set List Iterator as a second index. | |
| 234 | |
| 00:16:42,000 --> 00:16:48,000 | |
| Here is a position of the cursor in the index zero here, position of the cursor in the index one and | |
| 235 | |
| 00:16:48,000 --> 00:16:49,000 | |
| here is two. | |
| 236 | |
| 00:16:50,000 --> 00:16:54,000 | |
| So the next goal of the previous method should return number two. | |
| 237 | |
| 00:16:55,000 --> 00:16:59,000 | |
| And now you can see in the console output that two is returned. | |
| 238 | |
| 00:16:59,000 --> 00:17:02,000 | |
| Now I want to update this element. | |
| 239 | |
| 00:17:02,000 --> 00:17:06,000 | |
| I call set method and pass twenty two this method. | |
| 240 | |
| 00:17:06,000 --> 00:17:10,000 | |
| So the elements that was returned previously is substituted with twenty. | |
| 241 | |
| 00:17:11,000 --> 00:17:17,000 | |
| So my next goal of the next method should return twenty and he and console you can see at least Iterator | |
| 242 | |
| 00:17:17,000 --> 00:17:20,000 | |
| works exactly in the way we expect it to work. | |
| 243 | |
| 00:17:21,000 --> 00:17:27,000 | |
| In case I want to add an element with the help of this iterator, I call add method and the item is | |
| 244 | |
| 00:17:27,000 --> 00:17:29,000 | |
| inserted right before the cursor. | |
| 245 | |
| 00:17:30,000 --> 00:17:36,000 | |
| So in case I added twenty one, I can get it now by calling the previous method and here's twenty one | |
| 246 | |
| 00:17:37,000 --> 00:17:42,000 | |
| and remove mass at the last item that was returned either by next or previous method. | |
| 247 | |
| 00:17:43,000 --> 00:17:50,000 | |
| In this case after location of remove method twenty one will be removed and here we printed all elements | |
| 248 | |
| 00:17:50,000 --> 00:17:51,000 | |
| to console to prove this. | |
| 249 | |
| 00:17:52,000 --> 00:17:57,000 | |
| Now, I believe you have a full understanding of how Étretat enlisted radar works. | |
| 250 | |
| 00:17:57,000 --> 00:17:59,000 | |
| Let's recap what we have learned today. | |
| 251 | |
| 00:18:00,000 --> 00:18:02,000 | |
| Today, we learned what iterable is. | |
| 252 | |
| 00:18:02,000 --> 00:18:06,000 | |
| We also understand what Iterator is now. | |
| 253 | |
| 00:18:06,000 --> 00:18:12,000 | |
| We know the difference between these two interfaces when real examples, we saw how Iterator works in | |
| 254 | |
| 00:18:12,000 --> 00:18:13,000 | |
| this lesson. | |
| 255 | |
| 00:18:13,000 --> 00:18:18,000 | |
| We saw what exceptions might be thrown by iterator, including concurrent modification, exception, | |
| 256 | |
| 00:18:18,000 --> 00:18:23,000 | |
| illegal state exception, unsupported aliment exception and no such aliment exception. | |
| 257 | |
| 00:18:24,000 --> 00:18:29,000 | |
| We learned three types of iterator fail fast, fail safe and weakly consistent. | |
| 258 | |
| 00:18:30,000 --> 00:18:34,000 | |
| And at the end of the lesson we learned at least iterator and how it works. | |
| 259 | |
| 00:18:34,000 --> 00:18:36,000 | |
| Now let's review your homework. | |
| 260 | |
| 00:18:37,000 --> 00:18:43,000 | |
| In short, you have to coordinate exercises here, you have to implement Iterator and Lisa the for the | |
| 261 | |
| 00:18:43,000 --> 00:18:50,000 | |
| default Miley's class that you implemented in scoping her homework about lists, read attentively all | |
| 262 | |
| 00:18:50,000 --> 00:18:54,000 | |
| the requirements here I prepared for you method signatures and description of interfaces. | |
| 263 | |
| 00:18:55,000 --> 00:18:57,000 | |
| All you have to do is implement methods. | |
| 264 | |
| 00:18:58,000 --> 00:19:03,000 | |
| You will see here some requirements related to exceptions in case you are not familiar with exceptions | |
| 265 | |
| 00:19:03,000 --> 00:19:04,000 | |
| topic. | |
| 266 | |
| 00:19:04,000 --> 00:19:09,000 | |
| There is absolutely no harm because I wrote the line of code that you have to write. | |
| 267 | |
| 00:19:09,000 --> 00:19:15,000 | |
| One exceptions should be thrown that said, don't skip this homework. | |
| 268 | |
| 00:19:15,000 --> 00:19:17,000 | |
| It is important for understanding of the topic. | |
| 269 | |
| 00:19:18,000 --> 00:19:19,000 | |
| Thanks a lot for your attention. | |
| 270 | |
| 00:19:19,000 --> 00:19:21,000 | |
| See you in the next lesson. | |