Iterator and ListIterator

Start course
Overview
Difficulty
Beginner
Duration
2h 16m
Students
26
Description

In this course, we'll learn the Collection framework and the Map Interface.

Learning Objectives

  • Collection Framework vs Array
  • Hierarchy of Collection Framework
  • List interface - ArrayList Class
  • Iterator Interface
  • Set Interface
  • Queue and Dequeue Interfaces
  • Map Interface

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll talk about the Iterator in Java. A Java cursor is an Iterator, which is used to iterate or traverse or retrieve a collection or stream objects elements one by one. In this lesson, we will learn the Iterator and ListIterator. The ListIterator is an improved version of the iterator that we used in the list. Iterators in Java are used in the collections framework to retrieve elements one by one. It's a universal iterator as we can apply it to any collection object. By using iterator, we can perform both read and remove options. Iterator must be used whenever we want to enumerate elements in all collection framework implemented interfaces. The iterator is the only cursor available for the entire collection framework. 

Iterator object can be created by calling the iterator method present in the collection interface. You can see the syntax of the iterator on the slide. Here, myList is any collection object. Myiterator is of type iterator interface and refers to myList. Instead of the myList, you can use any collection object. There are basically three methods of the Iterator. The first method is the hasNext() method. This method returns 'True' if the iterator is able to move through the list. That is, the end of the list has not been reached yet and there is an element to iterate over. If the cursor is at the last element and there are no more elements to go, then it returns 'False'. The second method is the next() method. This method returns the next element in the iteration. And the last method is the remove() method. This method removes the next element in the iteration. 

This method can only be called once per call to the next method. Notice that the iterator only moves in one direction, that is, it starts from the first element and moves to the end of the list, but there is no backward movement. This is where the ListIterator comes into play, because listiterator has has previous and previous methods in addition to these methods. So, you can move forward or backward in the list using listiterator. Now, let's look at the methods of the listiterator. The hasNext(), next(), and remove() methods work just like the iterator. Another method is hasPrevious(). This method returns true if the iterator is able to move through the list, that is, the beginning of the list has not been reached yet and there is an element to iterate over. If the cursor is at the first element and there are no more elements to go, then it returns false. Another method is the previous() method. This method returns the previous element in the iteration. 

Another element is the previousIndex(). This method returns the previous element index or -1, if the ListIterator is at the beginning of the list. Another method is the set() method. This method replaces the last element returned by next or previous with the specified element. And the last method is add(). This method inserts the specified element into the list at the position before the element that would be returned by the next method. Okay, if you're ready, let's get some practice with the iterator. First, let's create a new class. I right click on the collections example package and select the new class options. The class name can be 'iteratorExample'. I'll check the checkbox for the main method and click the finish button. Okay, let's create an array list. List, integer, nums = new ArrayList. Now let's fill this list with numbers from 0 to 100. I'll use the for loop for this. We can fill this list quickly and easily with the for loop. Let's create the for loop, for(int i=0, i<= 100, i++). Inside the loop I write, nums.add(i). Thus the loop continues until the value of i is 101. When it reaches 101, the loop ends. 

Thus, all numbers from 0 to 100 will be added to the nums list in order. So, outside the loop, let's print the elements of this list. S.out.printIn.nums. Let's run and see. As you can see, the elements of the nums list range from 0 to 100. Okay, let's use the iterator. Let's do an example like that. We're going to remove numbers that are not a multiple of four, that is, numbers that are not divisible by four from the list of nums, and we'll get a list of numbers that are only divisible by four. We'll do this with the iterator. If you're ready, let's start. I'll create an iterator object. The type of this iterator will be integer because the nums list is an integer list. The name of this object can be numsIterator. After the equal sign I write, nums.iterator. Now I'll create a while loop, while(numsiterator.hasNext() ). In other words, if there is another element in the next where the iterator can go, then the condition will be true and the codes we wrote in the loop will work. Since there is no more element to go after the last element, the condition will be false and the loop will terminate. Now let's assign each element to the list to a new variable with the help of the next method of the iterator. So, I write, numsiterator.next(). 

Also, I'll assign this to a new variable. So at the beginning of the code I write, int num =. So, in each loop the next element will be passed to the variable num. Now let's create the condition here, If(num % 4 I=0). So if the element is not a multiple of four, that is, a number that is not divisible by four, we will remove that element from the list. To remove the element we'll use the remove() method of the iterator. So I write, numsiterator.remove(). Finally, let's print the final version of our list to the console. Outside the loop, I write, s.out.println.nums. Okay, let's run the code. As you can see, the new elements of the nums list are the multiple of four. Excellent. So this is the iterator. Now let's look at the ListIterator. I'll convert these lines to the comment line. Okay, let's create an object from the listiterator. ListIterator, integer, numsListIterator= nums.ListIterator. Now let's create a while loop, while (numsListIterator.hasNext() ). First, let's print the index number of the elements with the help of the next index method of the ListIterator. I write, numsListIterator.nextIndex(), and I'll assign it to a new variable. At the beginning of the code I write, Int Indexnum =. 

Now let's print them on the console. S.out.println.Indexnum + '' '' . Also this will be the print method, not println. So I want to show the index numbers side by side. Okay, now let's assign each element of the list to a new variable with the help of the next method of the ListIterator. So I write, Int num= numsListIterator.next(). So, in each loop the next element will be passed to the variable num. Now let's create the if condition here. If( num % 4 i=0). So if the element is not a multiple of four, that is, a number that is not divisible by four, this time I will convert these elements to one. So I write, numsListIterator.set (1). So, elements that are not a multiple of four will now be changed to one. Finally, I want to use a println method before printing the elements of the list, because we used the print method to print the index numbers in the loop. That is, the cursor is still on the same line. I'll use the println method here to move to the next line. Okay, let's run and see. This line is the original version of the nums list. This line is the index number of the elements, and in this line is the final form of the list. Notice that all numbers that are not a multiple of four have been changed to one. Yes, that's how we learned the concepts of iterator and ListItererator. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
409
Courses
35
Learning Paths
2

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics