Each collection in Java has an iterator object that can be used to traverse all the elements in the collection. One of the benefits of using the iterator is that it provides tools for walking through a data structure without having to understand the details of how data is stored.
- [Instructor] The Iterable Interface. Each collection in Java has an Iterator object that can be used to traverse all the elements in the collection. Meaning that it can navigate from one element to the next. One of the benefits of using the Iterator is that it provides tools for walking through a data structure. And it hides the details of how the data is stored. The Collection interface extends the Iterable interface. The Iterable interface in turn, has the following methods.
It has an iterator method and a foreach method. The iterator method returns an Iterator object which is simply an object that provides methods that allow you to access the objects in the collection one at a time. The iterator method returns an instance of the Iterator interface And here are some of the methods that you can use, hasNext. We've seen this before when were, reading information from a file. It checks to see if there is another element in the list.
Then we have the next, which will actually retrieve that element and remove to actually take an element out of the list. So once the collection is created, we can use this Iterable Interface to traverse or to walk through the elements in the data structure. In addition to the Iterable interface, there is also a ListIterator that defines some addition methods that are appropriate, for iterating through, list collections. So the ListIterator is actually used for list data structures, such as your link list.
While the Iterator interface only allows access to each element in order, starting at the beginning, the ListIterator adds functionality, so we can actually walk through the list, backwards. It includes the methods, previous, hasPrevious, and previousIndex. Let's take a look at how to use the Iterable interface. I'm going to switch over to NetBeans. There is a sample file called IteratorExample.java located in your chapter 02, 02_02 folder.
We're going to start in the main method by adding a collection object. So collection, I'll just call it collection, equals. I'm going to use my Arrays.asList again, and this time, I'm going to provide the colors of the rainbow, in my list. So, I'm going to do red, orange, yellow, green. I'm going to go to the next line, I'm going to hit enter. Put a comma there first, there we go.
Blue, indigo, and violet. Alright, so now I have my list created and it's called Collection. Let me go ahead and Import the library that I need, so I'm going to click on the light bulb again, and I'm going to click on Add import. Let me scroll down a little bit. Now, the next thing I want to do is, I want to be able to, you travers or walk through this list. And so I'm going to use the Iterator. I'm going to do, Iterator, space.
And I need to give my Iterator object a name, because remember the Iterator method, returns and Iterator object. So I'll call it, iterator = collection.iterator. Okay, and that's going to return an object that I can now use. This new object has the methods of next, hasNext and remove. So, I am now going to say, while the iterator.hasNext. Right, so as long as there is another one, in my list, okay.
What I want to do, is I just want to print it out. So, I'm going to do, iterator.next. So remember, hasNext checks to see if there is another element and next actually gets a copy of the element and moves the pointer to the next element. Alright, so now, I also need to import my Iterator. I'm going to go ahead and click on the light bulb, and add the import. And now I'm going to run my program. And we can see, the Iterator ran through and it printed out each element. Red, orange, yellow, green, blue, indigo and violet.
So the Iterable interface, is an integral part of using data structures, such as Array list, and Link lists, and queues and stacks. The final version, of all the programs in the examples that I'm showing you, is also included in your exercise files folder. This particular example, is in your chapter 02, 02_02. I've added another folder called Final. And that's where you can find the completed version of our program.
In this course, explore data structures in the Java language. Follow Peggy Fisher as she introduces you to several types of commonly-used data structures in Java. Peggy explores ArrayLists and LinkedLists, both of which implement the List interface, which extends the Collection interface and allows you to access list elements in a specific order. She also goes into the Vector, Stack, and Queue collection classes. To help you better understand these new concepts, Peggy wraps up the course with a challenge—creating a grocery list—and provides a solution for that challenge.
- Reviewing the types of data structures
- Working with the Collection interface and Iterable interface
- Working with ArrayLists and LinkedLists
- Using the Vector, Stack, and Queue collection classes