i really love your tutorials. though i am learning, java coding after 10 years in IT industry, your lessons make the basics very strong and then we can build on top of that. thank you from bottom on my heart and sharing your knowledge. hats off to you.
This was very well produced. You did a good job of taking slight deviations to explain other basics, while not diving too far into a rabbit hole on them - that is a difficult tight rope to walk. I also appreciate that you spoke in a slow and calming tone, that made it easier to ingest the information. You also did a good job giving links to the difference "chapters" within the video. That's a very helpful feature. I will be tuning in to more of your videos for certain.
If anyone wondering, why HashSet puts items into seemingly random order. HashSet generates an int value for each entry, to use it as a key. When item is inserted, the hash function is applied to it, to get an int value, and the item is put into corresponding array position. If we need to get an item from such set, or check if the item is present, the hash gonna be computed for the item, and corresponding position gonna be checked for the item. This provides us with almost constant time for search and insert operations, even on very large sets of data.
Thank you so much. That is such a wonderful explanation. I have been searching for a perfect tutorial on Iterable and Iterator for so long and finally, your video is amazing. It just cleared out a lot of doubts inside me. Keep going.
@@JakobJenkov Wow, I never knew details could hurt! I was looking for a deeper insight into java topics and your channel is the only decent one. I don't have to do tons of extra research after watching your videos. However other tutorials leave a lot of questions unanswered... You got me as sub. Thank you for doing this! :)
Awesome lesson! I really enjoyed that you went into details about the methods and along with your other Iterable interface video its been very helpful! Will continue to watch more of your content!
Thanks for the tutorial, it's really good! However, I do have one question. At minute 11:53, I'm not exactly sure how iterator manages to remove all the elements from the list. When we enter the while loop the first step is to call iterator.next(), the next() will return the "current" element (where the pointer is) and then move the "pointer" to the next one. In our case it would first return Jane, and move the pointer to Heidi. After that we call iterator.remove(). This is where my confusion comes from. The pointer is now at Heidi, but it managed to remove Jane. How is that possible?
Hi Dusan, the functionality of the Iterator.next() method is actually a bit different than what you describe. Calling the .next() method will move the Iterator to the next element - and return that element that it is now pointing to. When you call .remove() - it will remove the element the Iterator is currently pointing to - which was the last element returned by .next().
very useful tutorial, thank you. you have earned a sub! one question: what is the point of using the Iterator? Why can't we just use our normal for/for-each and while loops to iterate over a collection?
The normal for-loop (with a counter) only works for data structures where you know how many elements it contains + are able to access the elements based on their index. This is possible for arrays and List instances, but not for e.g. a Set or Map. You may know how many elements a Set or Map contains, but you cannot access these elements by an index. With a Map you access elements via a key, and in a Set you can only iterate the elements via an Iterator. The for-each loop actually uses an Iterator behind the scenes. So - when using the for-each loop you are actually using an Iterator. You can use the for-each loop with all data structures implementing the Iterable interface (see my video about the Iterable interface). Using an Iterator was the way to iterate collections without indexed elements - before the for-each loop was added to Java. Also, the Iterator enables you to delete elements from the underlying data structure without the Iterator throwing an ConcurrentModificationException the next time iterator.next() is called.
Perfect explained tutorials. One question. If I wanna iterate through the list, it looks easier to use for each, or for each lambda expression. If I understood correctly, the iterator is used when I wanna remove something.
Yes, if you can iterate the contents of the Iterator with the forEach() method, then that's easiest. But yes, if you want to remove elements from the Iterator and its underlying collection, you need to use the Iterator.hasNext() and Iterator.next() and Iterator.remove() methods.
@@JakobJenkov Question to you guys -- I am still catching up with Java 8 due to too much time away. Is it not considered better style to use Java 8's .removeIf() which will do that same thing but in a declarative fashion? It seems the past few years the "No explicit loops!" (which really means very few explicit loops, not NONE) has come to Java from C++. I know it does the same thing inside, but again, I got the feeling internal iteration is favored over external iteration in Modern Java from stuff I've watched lately. So, .removeIf( myPredicate ) here then?
The for-each loop uses an Iterator behind the scenes. So does the Stream API. I actually prefer using a standard for-loop with an index variable whenever possible. The advantages of the other methods are so small I don't really feel a big need to use them.
Hi! This tutorial is great! I have a questions: When we use iterator.previous in a loop, could we remove elements from a list.. we go backward, and the previous element still remain so there will not have a (next)previous exception, Am i right. Yes, you couldn't use the same iterator again and it's obvious you don't need to do this like that...but ?!!!
I don't want to seem rude, but I think your tutorials are a bit too long for the concept you are explaining. Maybe it's just me, but around 15 minutes to me is the right time to describe Iterators
My audience is very diverse. Some are complete beginners. Others already have some experience and some knowledge. Others are already very experienced. It is impossible to make videos that are equally suited for all these people. Many experienced developers are better off scanning quickly through some textual docs, as they can often easily pick up how something works based on their knowledge, e.g. from a different language with similar functionality, or maybe because they have already been a bit exposed to the topic before. An experienced developer might only need a 5 minute introduction to the Java Iterator interface. A complete beginner might need 19 minutes. Or - skips out after 13 minutes to come back another day and watch the rest - if ever. You say "around 15 minutes" - but don't mention where that specific number comes from. Is that the amount of time you have the patience to put into it? Or is there some other specific reason it's 15 minutes, and not 12 minutes, or 16 minutes? Typically, I have chapters in my videos so you can skip over the parts you already know. Also, remember that TH-cam enables you to speed up the playback of the videos. That way you can go faster through videos you find are a bit too slow for your learning speed. I hope that's good enough for you ;-)
I appreciate that you took the time to give time stamps as to what topics were being discussed in the video, saved me a bunch of time
Yearh - chapters are useful :-)
i really love your tutorials. though i am learning, java coding after 10 years in IT industry, your lessons make the basics very strong and then we can build on top of that. thank you from bottom on my heart and sharing your knowledge. hats off to you.
Glad you like them!
This was very well produced. You did a good job of taking slight deviations to explain other basics, while not diving too far into a rabbit hole on them - that is a difficult tight rope to walk.
I also appreciate that you spoke in a slow and calming tone, that made it easier to ingest the information.
You also did a good job giving links to the difference "chapters" within the video. That's a very helpful feature.
I will be tuning in to more of your videos for certain.
Thanks ! :-) ... got more videos coming! :-)
This is probably the best video of Java Iterator in TH-cam.
Thank You.
You are very welcome! :-) Happy to see that my video has helped you! :-)
If anyone wondering, why HashSet puts items into seemingly random order.
HashSet generates an int value for each entry, to use it as a key. When item is inserted, the hash function is applied to it, to get an int value, and the item is put into corresponding array position. If we need to get an item from such set, or check if the item is present, the hash gonna be computed for the item, and corresponding position gonna be checked for the item. This provides us with almost constant time for search and insert operations, even on very large sets of data.
Thank you so much. That is such a wonderful explanation.
I have been searching for a perfect tutorial on Iterable and Iterator for so long and finally, your video is amazing. It just cleared out a lot of doubts inside me.
Keep going.
You are most welcome !! :-) ... and thank you very much for your kind words :-)
Good quality content. Surprised your channel doesn't have more subs. Very helpful explanations with good examples.
Well, my content is often a bit more detailed than what many programmers are looking for... maybe that's why? I don't know :-)
@@JakobJenkov Wow, I never knew details could hurt! I was looking for a deeper insight into java topics and your channel is the only decent one. I don't have to do tons of extra research after watching your videos. However other tutorials leave a lot of questions unanswered... You got me as sub. Thank you for doing this! :)
Thank you Jakob! This tutorial is so helpful! You've really got me understood how an iterator works in a plain and detailed way, my savior on java🤩🥰
You are welcome !! I am happy my tutorial was helpful for you ! :-)
Awesome lesson! I really enjoyed that you went into details about the methods and along with your other Iterable interface video its been very helpful! Will continue to watch more of your content!
Great !! Happy my tutorials were helpful to you ! :-)
Ich Küsse dein Auge! Habe beim Panitz nichts verstanden! Toll erklärt
Vielen dank ! Viel lien von Dir !! 😉😊
Thanks for the tutorial, it's really good! However, I do have one question. At minute 11:53, I'm not exactly sure how iterator manages to remove all the elements from the list. When we enter the while loop the first step is to call iterator.next(), the next() will return the "current" element (where the pointer is) and then move the "pointer" to the next one. In our case it would first return Jane, and move the pointer to Heidi. After that we call iterator.remove(). This is where my confusion comes from. The pointer is now at Heidi, but it managed to remove Jane.
How is that possible?
Hi Dusan, the functionality of the Iterator.next() method is actually a bit different than what you describe.
Calling the .next() method will move the Iterator to the next element - and return that element that it is now pointing to.
When you call .remove() - it will remove the element the Iterator is currently pointing to - which was the last element
returned by .next().
Thanks for sharing your knowledge, Jenkov. By the way, may I ask something? I am curious the reason why you change your IDE from eclipse to VSCode.
You are welcome :-) I am still using IntelliJ IDEA. I am just using Darcula theme (dark theme).
very useful tutorial, thank you. you have earned a sub!
one question:
what is the point of using the Iterator? Why can't we just use our normal for/for-each and while loops to iterate over a collection?
The normal for-loop (with a counter) only works for data structures where you know how many elements it contains + are able to access the elements based on their index. This is possible for arrays and List instances, but not for e.g. a Set or Map. You may know how many elements a Set or Map contains, but you cannot access these elements by an index. With a Map you access elements via a key, and in a Set you can only iterate the elements via an Iterator.
The for-each loop actually uses an Iterator behind the scenes. So - when using the for-each loop you are actually using an Iterator. You can use the for-each loop with all data structures implementing the Iterable interface (see my video about the Iterable interface).
Using an Iterator was the way to iterate collections without indexed elements - before the for-each loop was added to Java. Also, the Iterator enables you to delete elements from the underlying data structure without the Iterator throwing an ConcurrentModificationException the next time iterator.next() is called.
@@JakobJenkov thank you! very helpful.
Perfect explained tutorials. One question. If I wanna iterate through the list, it looks easier to use for each, or for each lambda expression. If I understood correctly, the iterator is used when I wanna remove something.
Yes, if you can iterate the contents of the Iterator with the forEach() method, then that's easiest. But yes, if you want to remove elements from the Iterator and its underlying collection, you need to use the Iterator.hasNext() and Iterator.next() and Iterator.remove() methods.
@@JakobJenkov Thank You!
@@JakobJenkov Question to you guys -- I am still catching up with Java 8 due to too much time away. Is it not considered better style to use Java 8's .removeIf() which will do that same thing but in a declarative fashion? It seems the past few years the "No explicit loops!" (which really means very few explicit loops, not NONE) has come to Java from C++. I know it does the same thing inside, but again, I got the feeling internal iteration is favored over external iteration in Modern Java from stuff I've watched lately. So, .removeIf( myPredicate ) here then?
Is Iterator used much these days to iterate through a collection? Using something like a for each loop or the stream API seems more convenient.
The for-each loop uses an Iterator behind the scenes. So does the Stream API. I actually prefer using a standard for-loop with an index variable whenever possible. The advantages of the other methods are so small I don't really feel a big need to use them.
Delightfully presented :)
Thank you ! :-)
Very helpful and well explained, thanks a lot!
You are welcome - glad you liked it! :-)
Thanks a lot Jakob
not only your name is cool, your video was cool :)
Thanks :-D ... my name is cool? How so? :-D
Hi! This tutorial is great! I have a questions:
When we use iterator.previous in a loop, could we remove elements from a list.. we go backward, and the previous element still remain so there will not have a (next)previous exception, Am i right. Yes, you couldn't use the same iterator again and it's obvious you don't need to do this like that...but ?!!!
As far as I know, there is no previous() method on a Java Iterator, so what you are describing should not be able to happen.
Thank You SIr
Most welcome!
thank you! very helpfull for me!
Great to hear that! :-)
Thank you for this
You are welcome :-)
Thanks
You are welcome :-)
tnx
You are welcome ! :-)
I don't want to seem rude, but I think your tutorials are a bit too long for the concept you are explaining. Maybe it's just me, but around 15 minutes to me is the right time to describe Iterators
My audience is very diverse. Some are complete beginners. Others already have some experience and some knowledge. Others are already very experienced. It is impossible to make videos that are equally suited for all these people.
Many experienced developers are better off scanning quickly through some textual docs, as they can often easily pick up how something works based on their knowledge, e.g. from a different language with similar functionality, or maybe because they have already been a bit exposed to the topic before.
An experienced developer might only need a 5 minute introduction to the Java Iterator interface. A complete beginner might need 19 minutes. Or - skips out after 13 minutes to come back another day and watch the rest - if ever. You say "around 15 minutes" - but don't mention where that specific number comes from. Is that the amount of time you have the patience to put into it? Or is there some other specific reason it's 15 minutes, and not 12 minutes, or 16 minutes?
Typically, I have chapters in my videos so you can skip over the parts you already know. Also, remember that TH-cam enables you to speed up the playback of the videos. That way you can go faster through videos you find are a bit too slow for your learning speed. I hope that's good enough for you ;-)