Not just look at it later which helps in it creating elements these are an exception in the previous 2
lesson and we'll see how it can be prevented by using any. 3
And it there is simply an interface and to access an instance of a character Vinita in the IT DID IT 4
AND method that is declared in the interface which is from the Gelada lying package because the collection 5
interface extends the digital interface on collection implementations like the deadliest implement. 6
It did it at a method by extending it above that collection interfaces saying that its elements can 7
be triggered from each statement or request any object it takes to implement the table interface later 8
in the demo. 9
We will actually test this out as we can see here. 10
You did have an interface that's three minutes later for each and speculator you did it that is abstract 11
when the other two are different methods which are what I did in July 8. 12
So the interface provides a different implementation for these last two methods that are meant there 13
. 14
As mentioned earlier written an iterator which can be used for it reading the elements of the collection 15
internally for each statement also invokes this method for dating the collection elements. 16
Next is for each method and this method can be in-road if we want to perform certain action for each 17
element of the collection. 18
And this action would be a method in an object which is passed us input but for each method. 19
Hence we can see that the parameter name here is action. 20
So the method that is part of the input object will be invoked for each element of the collection and 21
the methods input will be the element itself. 22
So the method would how only a single parameter. 23
Now you can see that the parameter type for this for each method is consumer which is an interface that 24
was introduced in July. 25
It doesn't if it has a single abstract matter will accept. 26
So we need to pass either an instance of consumer which implements accepte method or we can pass lambda 27
expressions on method references which are Djala 8 features that we will discuss later. 28
So let's not worry about this method for now. 29
But to briefly look at it in our Demel tournament with the disputed rigor which is also ordered in Java 30
8 and we're also not going to discuss it for now but it seems to be mainly useful for splitting elements 31
of collection into partitions so that we can process those partitions using some interrupts. 32
So if we have elements which would run into several hundreds of millions then this method can be used 33
to partition those elements on unprocessed does partitions through some parallel competition. 34
That way it would be very efficient. 35
We may look at it later but it is definitely not important for now. 36
And here is likely to create an interface if you will that it did it in matter of our list Egerton's 37
an instance of this interface idlest basically has a nested class which implements this interface. 38
So an instance of that master class will be returned when it did it at a method is invoked. 39
It can be used to all of the elements of the collection we already know that. 40
But in addition it also lets you remove an element during their duration which we could not do in the 41
previous demo. 42
And the use skills for this could be that you may have to do with a list of elements from the database 43
. 44
Now you want to the most some of them which meet certain criteria. 45
In this case as we address the elements VIGEN be the ones which meet that criteria are needed it has 46
this format. 47
It's obviously remore would be used for removing an element forEachRamaining meaning that there is a 48
default method which is similar to the from each method and it did it all end of his let's actually 49
head over to eclipse to see how all these methods work. 50
OK. 51
Before we implement it today the related code. 52
Let's take a look at the exception that we got in the previous demo. 53
So here we are in the list Demopolis. 54
And here we are iterating through the elements of the order list and when we reach the element which 55
is the second element we are trying to remove that element and that's when we're getting this exception 56
called concurrent modification exception. 57
So let's just go ahead and run this program. 58
So here we are. 59
So when we run it when we reach element 9 the element 9 is getting branded. 60
And then when we tried to remove that element we are getting this exception called concurrent modification 61
exception. 62
We'll see how this can be prevented using a good actor. 63
So let me just comment this ok no one change I made in this method is rerouting the other list in the 64
earlier the more we were turning a wide. 65
So now we are reading and we are returning an hour elapsed and then the main matter. 66
Here we are. 67
So the the other is a miser getting an alias so be it assigning it to an other list here or list under 68
invoking that pneumatic call. 69
It did it demo. 70
And we are passing this list as an argument. 71
And here is the new method. 72
So let's just go ahead and finish this matter. 73
So the input parameter is list. 74
So we know that in order to get the data we need to enroll it in it and get there off idlest on the 75
list. 76
So let's do that. 77
So it's just an iterator here and its name it does it later. 78
Now this one would return an instance of it did it or interface. 79
And since the list has integers so the Jenrette notation would include integer. 80
OK now we need to iterate it unfolded it. 81
We can use either a white or a look but normally do it direct using an iterator wide loop is more cleaner 82
. 83
So let's use dot here. 84
All we need to do is say it greater DOT has Next we need to invoke the has an X method. 85
So Hasenfus McMurtry. 86
Either a true or false if it returns true. 87
It means that there are more elements to be evaluated. 88
Now if it returns false it means that we are done with it rating through all the elements. 89
OK so now let's assume that it has returned true. 90
That's right the body here. 91
So in order to get the next element we need to just invoke the next method. 92
And the next method would read and the next element. 93
But since this is this idlest has integers let's assign it to an integer here it's called it does a 94
element here. 95
OK. 96
So this would automatically unblocks and would assign it to the meadow. 97
And let's also bring this element here. 98
Now we know that if element equals nine we need to remove it. 99
And for that we can use the every moment third on the letter K and it's also printed here. 100
So let's go under on this. 101
So as you can see initially there are five elements of 1 9 8 6 and 1. 102
So after removing the element only four elements are there. 103
OK. 104
That is what is getting printed here. 105
So we no longer get to get that particular exception called concurrent modification exception. 106
So if you want to remove an element during a duration need to use the editor. 107
So that's the main benefit of it. 108
Now we also mentioned that in order to integrate this object list one for example in this because it's 109
an analyst has to implement the interface and we know that it implements they did have an interface 110
. 111
So if it doesn't implement a terrible interface then we would get a compilation error which means that 112
we cannot it the elements of the collection. 113
No let's say for example for instance we want to iterate through the list M-y itself. 114
OK so let's create an object here as you can see if we get a compile error it says can only iterate 115
over an array or an instance off of July Dr. Langhart. 116
It did. 117
But so this particular class this demo is not implementing it did happen. 118
So let's just go ahead and make it implement. 119
It did. 120
So let's go here. 121
Here it is. 122
Let me just uncommon this so it is implementing and it did have it but it was a compilation error because 123
we need to implement unimplemented methods which is that it did it. 124
So we need to implement a method here. 125
So let's do that. 126
I'd say public. 127
It should have done it better off and deeper 128
and could be delayed or monitored and it should return an instance of a director because right I would 129
only do later are in this case since we are just doing a demo on let's just see new idlest let's use 130
the it did it at off what it asked. 131
So in this case it would been ambulanced are now you can see that there is no more no more exception 132
here competition. 133
There is no computer that are here anymore. 134
So it is able do here. 135
So which means that the list is a little bit long. 136
OK. 137
So that's what you need to do. 138
You need to implement the interface if you want your objects to be iterated. 139
If you are elements to be triggered not like me change it back. 140
Good list. 141
So what it means is what it meant internally it invokes the the later method off the object that is 142
here and then it uses the instance that is returned by the director who did it with the elements. 143
OK so it is internally going to invoke the method of this particular object. 144
OK. 145
Now the reason why this concussion modification exception has generated is because it is actually generated 146
by the data that is internally written. 147
So on VH1 and it is a rating it is using the data internally to get the elements but externally here 148
within this method it is trying to remove some limit and it does not allow us to do that. 149
So it does not allow us to concurrently modify the underlying idlest. 150
So that's why it's called a concurrent modification exception. 151
And if we tried to do that the next method the next method within the iterator. 152
Through this concurrent modification exception. 153
For instance you may be able to remove this element but then in the next iteration Venite window for 154
each statement internally when it tries to use the next method then the underlying iterator built through 155
that exception the the next method with toady's concurrent modification exception because it does not 156
allow us to concurrently modify the audio list. 157
OK so now when it means modification it means structural modification and structural modification would 158
be either removing an element or adding an element of adding an element. 159
We would see in the next lesson when rediscuss listed character which is a sub interface of a character 160
. 161
So you can also add an element using glister detector. 162
So if you structurally modify either using art or a moment that's then we get this exception but replacement 163
is fine. 164
You can use Set method. 165
There is in list distributor which you will see again in the next lesson. 166
So that is fine as long as it is not concurrent modification. 167
We are good but if it is going to go and modification it is not possible to do it. 168
We will get this exception. 169
And if it has to be concurrently I mean if it has to be modified structurally modified then it can be 170
done only one way which is using it did it at itself. 171
So you can either use the you can use the remote method offer did it as we have seen and that gives 172
it just fine. 173
But if we are trying to modify the structure in any way other than using the methods of a director then 174
we will get this exception. 175
OK. 176
On such an iterator which gives a concrete modification exception is called as a fail fast. 177
It did it. 178
It's called Failed fast. 179
The reason is because it is failing fast it is failing quickly and all that general purpose implementations 180
like are a list link list and so on. 181
Do implement do support only fail fast. 182
It does get so they do throw this exception so that it can get an modification exception and do. 183
Why did we need to we can modify the structure thought it is using an iterator. 184
OK so that's the iterator. 185
That's all you need to know about it. 186
But we also looked at the for each method in the interface and we also look for each of the meaning 187
. 188
Ok so those I don't think we will use it very often. 189
For each Maybe we will use but I don't think for each of the meaning we will use it very often. 190
I don't see any uses for that. 191
So you might argue use it very much so but for the sake of completeness let's also look at those matters 192
. 193
So it's already here. 194
So here we are calling for each method and which is a declared an incredible interface. 195
OK. 196
So we would just say our IList object not for each. 197
And here for each of the input the input parameter is consumer. 198
But we said that it can be invoked using a matter reference. 199
So this is metadata for instance this is something new for us. 200
So here we are just passing a reference to the matter. 201
So the method is putting a line on this Prindle and motard system dot dot print Dalen will be invoked 202
for each element off the order list. 203
And that element will also be passed as input with the print on and printed on method. 204
OK so let's just go ahead and run it. 205
Actually let me Coleman this out here it's 206
not real good. 207
So as you can see these four elements are printed by them for each for each motard in location 1 8 6 208
and 1. 209
OK. 210
Now let me comment it out. 211
Now I haven't done a matter call your filter sorry a classical filter here and that filter glass also 212
has a method called Filter. 213
So we are here we are. 214
And it would print an element only if input. 215
But I mean if the value passed in what parameter is 1. 216
So it's just earlier we hope the print doesn't matter. 217
Here we are invoking the free delimiter of our own custom class that we have done. 218
OK so far. 219
When we look at the reference we use this Colons double 2 colons here. 220
So let me just print it. 221
But in this case it would print only 1 and 1 and that's because the filter method is of the four elements 222
at this printing only one. 223
OK. 224
Because there are 1 8 6 and 1. 225
So here the filter that we are applying is 1 if we are saying if it is I equals one or need an open 226
ended. 227
So that's the one on the third one as we can either posit matter differences or lambda us which are 228
Djala 8 features are we can also pass an instance of consumer. 229
Can I read is giving a compilation error because we need to implement the consumer interface. 230
Ok so we need to implement it because the filter here is not implementing it. 231
So let me uncommon did. 232
Now it implements the Kansi you want interface which has only a method called accept. 233
So we need to do this method gets invoked now. 234
So it is also doing the same thing printing the elemental if I is equal to 1. 235
So as you can see there is no more compilation error. 236
I don't need that element one is getting another. 237
OK so even in this case when we are invoking the method through using a method reference in dentally 238
there some magic. 239
I think it's dynamic by using dynamic proxy or whatever it internally in the byte code. 240
We will be passing an instance of guns you would get passed on that instance would include the accepte 241
method. 242
So the instance will be created everything will be done internally itself inside the bytecode the compiler 243
will do that. 244
So so so that they can get off in Bonaly so it will still pass an instance of consumer even though it 245
is a matter reference. 246
So that is done internally but let's not worry about it. 247
Now finally let's look at for each of the many. 248
So it's a bit confusing this for each motard on for each of the meeting mattered since they have similar 249
names for each is inside is defined by terrible. 250
But for each any meaning is defined by the interface. 251
It's also a Djala 8. 252
And here we are in working the odd matter in the filter and the augmented. 253
All it does is it adds this value 7 to the element unfussy each new meaning. 254
All it does is. 255
So so far we have two elements. 256
The first element and the second element was nine here. 257
So nine is that the matter is activated and once we reach nine we are removing it. 258
And for all the subsequent elements 8 6 and 1. 259
So we are saying for each remaining element in will the add method. 260
So basically the remaining three elements will be activated by this method itself. 261
And so the wild method will not continue to exit. 262
OK so all the remaining elements will be directed by using through this single location. 263
So that's where it says it is Galdos for each remaining again for each here for each method we do for 264
all of the elements but for each remaining is all it did it through all the uninitiated elements the 265
items that were not iterated so far. 266
So let's just go ahead and run this. 267
So it is for each of the meaning here as printing Rufin because 7 was added to 8 in the Agde method 268
and 6 7 was added to 6 which resulted in 13 and 7 was added to 1. 269
So the last three elements for the last three elements this method I'd got I got involved. 270
So that's for each remaining but I don't think you will use it very much. 271
So that's about it. 272
So that's the iterator. 273
The main advantage as you can direct and at the same time you can also remove the elements and we also 274
looked at for each unflushed each remaining richer Gela 8 features. 275
Now one thing I forgot to mention is that the location of that of a moment there must be must be preceded 276
by the invocation of next method. 277
OK so only after you invoke the next method will you be able to invoke the right moment. 278
Otherwise we get a compilation error because that implementor would remove the element that was returned 279
by the next method. 280
OK. 281
So if you can be taught in walking next then we get a compilation error. 282
So for instance let's put in the statement here. 283
It did it or not remote. 284
So this is the second amendment that would throw an exception here that you are giving us an illegal 285
state exception. 286
OK. 287
So the flustering moment there was fine because it was secured by next but the second remote method 288
is giving us this exception OK because it has to be preceded by next. 289
So that's that's one thing I wanted to mention. 290
So that's about it. 291
Thank you. 292
And happy coding. 293
Or you can find the find the code in the resources section. 294
Thank you