We didn't go over iterators in my data structures class I just took. I know what they were but didn't realize how useful they can be until now. Well worth the time to show us a good way and a better way. Props to you!
If you have fundamentals under your feet, Derek Banas is the best teacher ever. But if you don't, these videos are the real pain. I'm glad I can watch these videos today with a joy.
I am a big fan of your videos, but this video does NOT teach the iterator pattern , rather it uses the existing java's Iterator. Looks more like an application of the Adapter pattern. The video should have defined an Itertator interface and made all the Song lists implement that iterator interface.
thank you so much buddy for the videos you saved me from my prof video which is useless 2 hours with a 22 mins full of knowledge , i really appreciate your work my friend
The thing with design patterns is that people tend to never use them, or they over use them. Are they valuable? I'd say they definitely are if only to make systems easier to describe to others. When you work on a large project meaning thousands of lines of code you'll see the value in describing 15 lines of code with a pattern name. On top of that, your code will be very readable by others that know patterns
Thank you :) I do my best to fill in the niche subjects that others don't. I'll be covering every Java framework by the end. I'm working on a code refactoring tutorial that is getting quite large, so you can see I'll be covering Java at a deep level. I also have tutorials on graphics engines created in combination with c in the works. Everything J2EE will be covered along with everything else you guys ask for. Thanks for the request
Good video. The pragmatic approach to implementing java iterator interface step by step was exceptional + advice on good coding practices was well-received.
I use your videos as reference to remember stuff, witch is excellent. If I have to learn more details stuff, actually I go read a book about it, and these videos really help to make things clear. Keep up de good work.
It's unfortunate that you didn't provide an UML with it to explain the specific classes that you've made and put it side by side to show what class is what in the UML structure of a valid Iterator pattern structure.
+Derek Banas, I have seen your other video which was all great. but i think in this code you have written for explaining iterator is not complete. because you didn't create iterator from the scratch like other design pattern video you did. thus its confusing.
What if I want to iterate over the elements of a nested ArrayList. E.g. I have an ArrayList that I call "names" and I want to iterate over the strings. With your solution the createIterator() method would return names.iterator() which would be an interator which iterates over ArrayList objects. But I want to iterate over the strings itself, not over the ArrayLists which contain the strings. Can this be solved?
I didnt get you why implemented Iterator interface and then return java list.iterator from SongOf80s etc classes. Shouldn't you be returning SongIterator implemenation instance instead ?
to Tritone as you see the keyword "implement" from the video, the iterator is a kind of interface. so you can check what kind of method in this awesome interface for the collections. of course in the java API page in the oracle website(or sunMicros or whatever lol)
I don't actually think it has to do with a COLLECTION of objects rather it's just a way to access and cycle to the next item in a SEQUENCE. For example, you can have an iterator to go through the Fibonacci sequence and all the iterator does it give you the next number in the sequence. For example if you begin with 1, when you call next on that iterator you get 1, call next again and you get 2 and so on and so forth. The actual sequence doesn't need to be in a collection but it could, it rather abstracts a way to get items in sequence.
Hello Derek, Thank you for this video, I was trying to implement this pattern using C++ (STL) , but I am facing issues. The main reason what I am observing is, in STL, there is no single base class for different containers (vectors, maps, deque etc) iterators. So I am unable to implement a common function to iterate through all containers using same iterator type and ending up in writing different print functions for different containers. Could you please suggest any work around for this problem in C++
What is the advantage of implementing iterator pattern if I simply can retrieve Iterator object from the list? Let's say I have LinkedList called birds so I can do: Iterator iterator = birds.iterator() and then I am able to use functions like hasNext() or any other?
But what should be done when classes are compiled and can't be modified? Bunch of adapter patterns? By studying Software Engineering i become allergic to changing tested code :)
Im a nub, But!-- have you tried @SuppressWarnings("Iterable")? I think "Serializable" was one.. try case sensitive and grammatic siblings n cousins.. :)
Absolute, an awesome and clean explanation. However, I think we can rename the interface to SongIterable (containing the createIterator method) to make it sounds like the collection (e.g. SongOfThe70s or SongOfThe80s) is iterable
I understand you're very good at making videos but I feel like you need 2 critics if you indeed want to TEACH people something and not just show them how its done. Your explanation is way too brief and really directed towards your current example. Maybe say why this is useful when to use it how it overall should be written.. stuff like that. And 2nd of all you do this too fast tldr : I dont feel like I learnt something, i feel like I learnt this exact example..
I agree that I could do many videos on each pattern. I didn't at the time because I was still learning how to better do tutorials. Now I have a bigger audience. I listen to what they are struggling with and then I recover those topics as tutorials continue. I'm very open to criticism. I love when people tell me they need further help in the comments. That completely controls what I cover in the next videos. I also know I move pretty fast. I want to make original videos. That made me decide to move quicker, but to also describe what I was doing and thinking more then I've seen in other tutorials. I constantly try to improve my speed. Thank you for the input :)
+Andrew Daly Java’s generic programming does not apply to the primitive types, such as "int" since generic data structures can only hold objects, while values of primitive type are not objects. However Java's wrapper classes such as "Integer" behave the same as ints but they are actually objects, and thus can be used in generic types like Iterators and Hashtables. Check out this SO thread: stackoverflow.com/questions/13216314/hashmap-does-not-work-with-int-char
Here is an implementation that does it using Java Iterator and by defining your own Iterator Interface. CODE: import java.util.*; interface SongIterator { boolean hasNext(); SongInfo next(); } class ConcreteSongIterator implements SongIterator { List l; int counter; ConcreteSongIterator(List l) { this.l=l; counter=0; } public boolean hasNext() { return (counter
Hello Derek, I liked your video. would we please recommend some books to learn design patterns in detail along with some good implementation for each on of it. Thanks for the video again.
Rupali Pawaskar Thank you :) The head first book is good for a simple explanation and then the book Design Patterns: Elements of Reusable Object-Oriented Software covers everything in detail.
Hello, Good video, but it would have been great to show that you can type your iterator. And also mentionning that it is very useful to go through a list and at the same time removing some elements, without getting a ConcurrentModificationException :)
hello Derek, Thanks for your tutorials, both this one and the UML are my favorite ! However, when I try to run this code, this error occurred: ------------------------------------------------ Exception in thread "main" java.lang.NullPointerException at Iterator.DiscJockey.showTheSongs2(DiscJockey.java:66) ------------------------------------------------ It's Iterator Songs70s = iter70sSongs.createIterator(); Do you have any clue to solve it? Sorry to trouble and thx again !
+Derek Banas Sorry to trouble you ! In fact, I've compared the code but still could not find the problems then I asked you. So far three Design Patterns' code: Iterator, Abstract Factory and Interpreter can not run successfully in my environment. Is it possible the difference of the JDK or something else ? Thanks a lot !
I don't like this video. The example ads way too much overhead that distracts from the iterator pattern and in the end you don't even really implement it .
Can you help me to implement in songsofthe70s.java Iterator class something like that? @Override public Iterator CreateIterator() { Iterator it = new Iterator() { private int currentIndex = 0; @Override public boolean hasNext() { return currentIndex < currentSize && bestSongs[currentIndex] != null; } @Override public Type next() { return bestSongs[currentIndex++]; } @Override public void remove() { throw new UnsupportedOperationException(); } }; return it; } }
That is high praise! I can't think of many people who could drive people to action like John Lennon did. Thank you SawMan :)
We didn't go over iterators in my data structures class I just took. I know what they were but didn't realize how useful they can be until now. Well worth the time to show us a good way and a better way. Props to you!
If you have fundamentals under your feet, Derek Banas is the best teacher ever. But if you don't, these videos are the real pain. I'm glad I can watch these videos today with a joy.
I am a big fan of your videos, but this video does NOT teach the iterator pattern , rather it uses the existing java's Iterator. Looks more like an application of the Adapter pattern.
The video should have defined an Itertator interface and made all the Song lists implement that iterator interface.
thank you so much buddy for the videos you saved me from my prof video which is useless 2 hours with a 22 mins full of knowledge , i really appreciate your work my friend
I never get tired of liking your video.
It's 2020 in the middle of a pandemic. Got nothing to do so im rewatching the whole playlist 😂
same Lester
The thing with design patterns is that people tend to never use them, or they over use them. Are they valuable? I'd say they definitely are if only to make systems easier to describe to others. When you work on a large project meaning thousands of lines of code you'll see the value in describing 15 lines of code with a pattern name. On top of that, your code will be very readable by others that know patterns
The patterns provide coding perfection! It just takes a bit to get there
Thank you :) I do my best to fill in the niche subjects that others don't. I'll be covering every Java framework by the end. I'm working on a code refactoring tutorial that is getting quite large, so you can see I'll be covering Java at a deep level. I also have tutorials on graphics engines created in combination with c in the works. Everything J2EE will be covered along with everything else you guys ask for. Thanks for the request
I'm very happy to be of help :) I wish you the best of luck on your interviews and new job.
Woaaaw very interesting video ! I'm a french studients and now, with your tutorial, I understand better this DP ! Thanks a lot
Thank you :) I'm very happy to cover these niche topics
Thank you a lot. It is the best tutorial I have ever watched.
Thank you very much :) I'm happy you liked it
Derek it was a good watch. I felt this is amazing pattern. Hope to learn more patterns from your videos
A link to the source code is in the description for the video. I hope it helps and feel free to ask questions
I'm going to cover algorithms and data structures again soon. Sorry i can't make the video now
Thank you :) That is very nice of you to say that
hmmm…how does the Iterator Pattern actually work? It would have been better if you showed how to implement one instead of using the API.
Yea, kinda bad approach. Iterator is one of the most used patterns and is really useful to understand how it works.
Can't you just go into the sources of iterator class?
@@ppraisethesun ikr. Seriously ppl want everything handed to them
@@smartwerker I mean it's a tutorial, how is it weird that people want to learn how it actually works by clicking on this video?
Easy and good explanation of design patterns , covers from beginner to novices.
Hey you earned a subscriber.
Thank you very much :)
Good video. The pragmatic approach to implementing java iterator interface step by step was exceptional + advice on good coding practices was well-received.
Thank you very much :)
Thank you very much for the kind message :) You're very welcome
You're very welcome :) I'm glad to have helped
21:38 - why didn't you type the Iterator as Iterator? Then you don't have to cast.
do someone understand 13:22 what does he do there? i cant type in the same in my programm...
I use your videos as reference to remember stuff, witch is excellent. If I have to learn more details stuff, actually I go read a book about it, and these videos really help to make things clear. Keep up de good work.
Thank you very much :) I always considered my videos to be a good addition to either book or classroom study.
at 6:14 bestSongs[arrayValue] = song , song is undefined ?
What awesome channel!!! 🤯 Thanks!!!!
Thank you very much :)
cool video, dude. u explain better than my professor
Thank you :) It is very nice of you to say that
You're very welcome :) I do the best that I can
It's unfortunate that you didn't provide an UML with it to explain the specific classes that you've made and put it side by side to show what class is what in the UML structure of a valid Iterator pattern structure.
Thanks! Your videos have really helped me prepare for interviews!
+Derek Banas, I have seen your other video which was all great. but i think in this code you have written for explaining iterator is not complete. because you didn't create iterator from the scratch like other design pattern video you did. thus its confusing.
+Global S Sorry about that. I hope to revisit design patterns.
Thank you for your videos, I watched all of them and they really helped me understand design patterns.
Thabo Teffu You're very welcome :) I'm glad I could help
What if I want to iterate over the elements of a nested ArrayList.
E.g. I have an ArrayList that I call "names" and I want to iterate over the strings. With your solution the createIterator() method would return names.iterator() which would be an interator which iterates over ArrayList objects. But I want to iterate over the strings itself, not over the ArrayLists which contain the strings. Can this be solved?
Thank you for the very clear explanation of the iterator pattern!
Thank you :) yes, I should have used more diagrams.
What if i want the array to be dynamic? There you said size is 3, what if I want size 50 but there's some empty spots?
I didnt get you why implemented Iterator interface and then return java list.iterator from SongOf80s etc classes. Shouldn't you be returning SongIterator implemenation instance instead ?
to Tritone
as you see the keyword "implement" from the video, the iterator is a kind of interface. so you can check what kind of method in this awesome interface for the collections. of course in the java API page in the oracle website(or sunMicros or whatever lol)
finally, i understand why i need to use an iterator :P. Thanks!
I don't actually think it has to do with a COLLECTION of objects rather it's just a way to access and cycle to the next item in a SEQUENCE.
For example, you can have an iterator to go through the Fibonacci sequence and all the iterator does it give you the next number in the sequence. For example if you begin with 1, when you call next on that iterator you get 1, call next again and you get 2 and so on and so forth. The actual sequence doesn't need to be in a collection but it could, it rather abstracts a way to get items in sequence.
Thank you :) You're very welcome.
+Derek Banas : concept wise clear, but transformation into C++ is not easy through with the given sample. Any suggestions?
Subscribed :) ! best tutorial seen so far!
Thank you :)
Java gives a warning when I try to use Iterator in the interface. Is there any workaround to it?
Hello Derek,
Thank you for this video, I was trying to implement this pattern using C++ (STL) , but I am facing issues. The main reason what I am observing is, in STL, there is no single base class for different containers (vectors, maps, deque etc) iterators. So I am unable to implement a common function to iterate through all containers using same iterator type and ending up in writing different print functions for different containers. Could you please suggest any work around for this problem in C++
Thank you for the video! It was very understandable and has helped me a lot 😃
What is the advantage of implementing iterator pattern if I simply can retrieve Iterator object from the list?
Let's say I have LinkedList called birds so I can do:
Iterator iterator = birds.iterator()
and then I am able to use functions like hasNext() or any other?
It's usefull here to encapsulate the way you get the iterator. For example, it differs with the map, you have to call values() first.
THX, Great explanation, Do you have by any chance the uml diagram of this implementation?
Thank you :) Sorry, but I don't
You're very welcome :)
Very simple and smart ^_^ thanks!
Thank you :)
Thank you :)
I was a bit confused. How did you make objects from an interface ? :/
+K Badsha Sorry if it was confusing. We implement the interface SongIterator, but we can't instantiate it
I understand it now :)
very helpful! thank you!
But what should be done when classes are compiled and can't be modified? Bunch of adapter patterns? By studying Software Engineering i become allergic to changing tested code :)
Hey Derek,
What is the best way to get rid of the type warnings on the iterators?
Im a nub,
But!-- have you tried @SuppressWarnings("Iterable")?
I think "Serializable" was one.. try case sensitive and grammatic siblings n cousins.. :)
wjrasmussen666
Change the function in interface SongIterator from :
public Iterator createIterator();
To :
public Iterator createIterator();
Hi Derek,
Fantastic explanation :), thank you ..
Bijoy Baral Thank you :) Your welcome
Absolute, an awesome and clean explanation. However, I think we can rename the interface to SongIterable (containing the createIterator method) to make it sounds like the collection (e.g. SongOfThe70s or SongOfThe80s) is iterable
that was awesome code man thanks it helped me
PRATIK RAJPUT Thank you :) I'm glad it helped
Good. Just a suggestion: you should show diagrams at the begining of the each pattern explanation.
Thank you.
I understand you're very good at making videos but I feel like you need 2 critics if you indeed want to TEACH people something and not just show them how its done. Your explanation is way too brief and really directed towards your current example. Maybe say why this is useful when to use it how it overall should be written.. stuff like that. And 2nd of all you do this too fast
tldr : I dont feel like I learnt something, i feel like I learnt this exact example..
I agree that I could do many videos on each pattern. I didn't at the time because I was still learning how to better do tutorials.
Now I have a bigger audience. I listen to what they are struggling with and then I recover those topics as tutorials continue.
I'm very open to criticism. I love when people tell me they need further help in the comments. That completely controls what I cover in the next videos.
I also know I move pretty fast. I want to make original videos. That made me decide to move quicker, but to also describe what I was doing and thinking more then I've seen in other tutorials. I constantly try to improve my speed. Thank you for the input :)
Why Hashtable and not Hashtable ?
+Andrew Daly Java’s generic programming does not apply to the primitive types, such as "int" since generic data structures can only hold objects, while values of primitive type are not objects.
However Java's wrapper classes such as "Integer" behave the same as ints but they are actually objects, and thus can be used in generic types like Iterators and Hashtables.
Check out this SO thread: stackoverflow.com/questions/13216314/hashmap-does-not-work-with-int-char
thank you, interesting.
Hi Derek. I am unable to open the code links. It hangs indefinitely. Could you please help?
Works by explicitly prefixing in the redirected URL - www.newthinktank.com/2012/10/iterator-design-pattern-tutorial/
Great explanation! Thanks a lot.
nice! congrats from Brazil
Thank you :)
Thank you so much
Thank you :) I'm happy you like them
you are a great guy
Great!! Thanks for your time and effort!
Hey Derek, could you do a video on Circular Buffer?
Here is an implementation that does it using Java Iterator and by defining your own Iterator Interface.
CODE:
import java.util.*;
interface SongIterator
{
boolean hasNext();
SongInfo next();
}
class ConcreteSongIterator implements SongIterator
{
List l;
int counter;
ConcreteSongIterator(List l)
{
this.l=l;
counter=0;
}
public boolean hasNext() {
return (counter
Nice1 saved me 150+ lines of code & a Sandwich.
Hello Derek,
I liked your video. would we please recommend some books to learn design patterns in detail along with some good implementation for each on of it.
Thanks for the video again.
Rupali Pawaskar Thank you :) The head first book is good for a simple explanation and then the book Design Patterns: Elements of Reusable Object-Oriented Software covers everything in detail.
thank you , well explained
I'm happy it helped :)
hello Derek,
It's Davix again. I want to know do you have a tutorial about shallow and deep cloning in java? Thx !
Hello,
Good video, but it would have been great to show that you can type your iterator.
And also mentionning that it is very useful to go through a list and at the same time removing some elements, without getting a ConcurrentModificationException :)
Pattern starts at 15:00
Congrats :)
Really useful video, Thank you.But i couldn't download your full presentation from Drop Box, if it is possible share it for me.thanks again .
Mohsen Moeini Thank you :) I have all of the code here www.newthinktank.com/2012/10/iterator-design-pattern-tutorial/
Podium !
Your the John Lennon of Tutorial !
thank you
+Sahar Mu. Thank you :) You're very welcome
hello Derek,
Thanks for your tutorials, both this one and the UML are my favorite !
However, when I try to run this code, this error occurred:
------------------------------------------------
Exception in thread "main" java.lang.NullPointerException
at Iterator.DiscJockey.showTheSongs2(DiscJockey.java:66)
------------------------------------------------
It's
Iterator Songs70s = iter70sSongs.createIterator();
Do you have any clue to solve it?
Sorry to trouble and thx again !
+davix Your welcome :) That line of code is fine. Try comparing your code to mine www.newthinktank.com/2012/10/iterator-design-pattern-tutorial/
+Derek Banas
Sorry to trouble you ! In fact, I've compared the code but still could not find the problems then I asked you. So far three Design Patterns' code: Iterator, Abstract Factory and Interpreter can not run successfully in my environment. Is it possible the difference of the JDK or something else ?
Thanks a lot !
you know you are the best right ? how about tuts about frameworks ,building java application..
should i still buy the book even after your tutorial?
Vasupol Chatmethakul The GOF book is a good investment because it covers patterns in more detail then I do here.
is there source code?
I don't like this video.
The example ads way too much overhead that distracts from the iterator pattern and in the end you don't even really implement it .
great ...thanks..
great tuts :-)
Can you help me to implement in songsofthe70s.java Iterator class something like that?
@Override
public Iterator CreateIterator() {
Iterator it = new Iterator() {
private int currentIndex = 0;
@Override
public boolean hasNext() {
return currentIndex < currentSize && bestSongs[currentIndex] != null;
}
@Override
public Type next() {
return bestSongs[currentIndex++];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
return it;
}
}
Thanks!!!!!
You're very welcome :)
Derek Banas You helped me to understand what my prof wasn't able to explain :)
Weissiiii Thank you :) I'm happy that I could help.
I really just want to hear you say "Drugs are bad mmmmkay?"
yea good point .
any way thank Derek your doing a great job (Y)
I'm going crazy because the 70's songs won't output
lol nevemind just did a dumb mistake that I couldn't find for 30 minutes straight
I LOVE YOU
Thank you :)
I just watched 23 minutes of video to see how to use existing java iterator. I was hoping to see how exactly an iterator design pattern is realized .
finally, english
Haha, it was painful to watch so much copy paste code what you did before doing it with Iterator.
100#
at 6:14 bestSongs[arrayValue] = song , song is undefined ?
You're very welcome :)