Transcript from the "Collection Iterators" Lesson
>> So a common task with collections is to loop through the elements of the collection, and one way that we can do this is with iterators. Now, the collection interface does provide an Iterator for you. So we are going to use an iterator to loop through our set of fruit.
[00:00:20] So in the collections demo, let's go to that set demo method, I'm gonna comment out these other things. All right, so in this set demo, Let's, Before any of our print statements, let's go ahead and add the looping here. So the way that you get the iterator, I'm gonna use var, [LAUGH] and we'll say i = fruits, the name of the collection, dot iterator.
[00:01:06] And now we have an iterator object which we can use to loop through the fruits. So the way you would do that is with a loop of your choice, while is a good option, especially with iterators because you wanna say keep looping while there's more elements to loop through.
[00:01:24] And the iterator provides this hasNext method that returns a Boolean. So this basically means, are there any more elements in the array? You'll say yes, and then you carry on, all right? In order to get to the next element in the array, we would use the iterator and just say next, and that will give us the next object in the collection.
[00:01:56] All right, so I'm going to comment out everything after that so we could just focus right here. But let's go ahead and run this class and notice it printed out each of the fruits that we added, On a new line. So before when we printed this out remember it printed it in square brackets and it printed essentially the entire collection or here is printing the elements, not the collection itself.
[00:02:35] So again, collection provides iterator method gives you an iterator the key methods on iterator are hasNext which you could put in a while loop. Let you know should I be looking any more, are there more elements here that will be a true or false? And in order to access the next element you just see i.next that'll give that to you.
[00:03:04] Questions so far? Okay, I'm gonna uncomment this out. All right, so the iterator method is provided by the collection interface. So it's available on all the collections. I showed you it on set, but that same exact code would work on list, it would also work on queue. Where it would not work is on map because again, map is part of the collection framework, but it does not inherit from the collection interface, so it doesn't have access to that method.
[00:03:50] Now we can still do this with map, we just have to use another technique. And that is the entrySet method that exists on the map itself. So with the other collections we said, we used the collection name, and then we said, .iterator, you don't have dot iterator, but we do have dot entrySet on a map, okay?
[00:04:15] Now given the set, you can then use the iterator method to loop through the elements. So once we have the set, I say give me an entry set. What this does is take the map elements and turn them into a set collection, and then I'll set, set inherits from collection interface.
[00:04:37] So now you have access to the iterator method. Notice my while is the same, I do i.hasNext here, right? And then when I wanna get the next element, I say i.next, but what am I doing there with that map entry in parentheses? Casting, right? So we've seen this before, I'm taking the next element, I'm casting it to a Map.Entry element.
[00:05:08] With the map.entry, I now can get the key, and I can get the value. So this is not the most fun thing to do, [LAUGH] It's not as straightforward as those other collections, but it is here. This fortunately is not the only way to loop through things, but this is comparable to how iterator works.
>> With this kind of he looked down upon to like,
>> I would look down upon it probably, Yeah, I'm pretty certain I would flag this in a cold review and say, why are you, [LAUGH] doing this? Why did you spend your life doing this? So I'll show you some more effective ways, [LAUGH] to get to what you need.