Java - Iterator

<<Previous

Next >>




Iterator

It is used to traverse through the elements of a collection in forward direction and it has methods to remove element as well.


	boolean	hasNext()
	E	next()
	default void remove()

ListIterator

It is used to iterate through a List in either direction. It has methods to add element and remove element.

List Iterator has the following methods:

	void	add(E e) - To add elements to a list
	boolean	hasNext() - returns true if the list has a next element
	boolean	hasPrevious() - returns true if the list has a previous element
	E	next() - returns the next element
	E	previous() - returns the previous element
	void	remove() - removes the current element
	void	set(E e) - sets the current element with the value provided

Example



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

	public class ExampleListIterator {
			public static void main(String a[]){
				Integer array[] = new Integer[]{10,5,6,3,91,200};
				List arrayList = new ArrayList<>(Arrays.asList(array));
				ListIterator itr= arrayList.listIterator();
				System.out.println("Using boolean  hasNext()");
				System.out.println("and E next()");

				while(itr.hasNext()){
					System.out.print(itr.next()+ " ");

				}

				System.out.println("\n");
				System.out.println("Using boolean hasPrevious()");
				System.out.println("and E previous()");
				while(itr.hasPrevious()){
					System.out.print(itr.previous()+ " ");

				}
				System.out.println("\n");
			}

		}

Output

Using boolean  hasNext()
and E next()
10 5 6 3 91 200

Using boolean hasPrevious()
and E previous()
200 91 3 6 5 10

Example 2



	import java.util.ArrayList;
	import java.util.Arrays;
	import java.util.List;
	import java.util.ListIterator;

	public class ExampleListIteratorAddRemove {
			public static void main(String a[]){
				Integer array[] = new Integer[]{10,5,6,3,91,200};
				List arrayList = new ArrayList<>(Arrays.asList(array));
				ListIterator itr= arrayList.listIterator();

				int index=0;

				System.out.println("Using void  add(E e)");
				while(itr.hasNext()){
					index++;
					itr.next();
					if(index==3)
					{
						itr.add(5000);
					}

				}
				System.out.println("After Adding an Element 5000 to the list...");

				System.out.println(arrayList);

				itr= arrayList.listIterator();
				System.out.println("\n");
				System.out.println("Using void remove()");
				index=0;
				while(itr.hasNext()){
					index++;
					itr.next();
					if(index==2)
					{
						itr.remove();//removes 5
					}

				}
				System.out.println("After removing the element at index 2 in the list...");

				System.out.println(arrayList);


			}

		}

Output


Using void  add(E e)
After Adding an Element 5000 to the list...
[10, 5, 6, 5000, 3, 91, 200]

Using void remove()
After removing the element at index 2 in the list...
[10, 6, 5000, 3, 91, 200]

<<Previous

Next >>







Java - Iterator

<<Previous

Next >>




Iterator

It is used to traverse through the elements of a collection in forward direction and it has methods to remove element as well.


	boolean	hasNext()
	E	next()
	default void remove()

ListIterator

It is used to iterate through a List in either direction. It has methods to add element and remove element.

List Iterator has the following methods:

	void	add(E e) - To add elements to a list
	boolean	hasNext() - returns true if the list has a next element
	boolean	hasPrevious() - returns true if the list has a previous element
	E	next() - returns the next element
	E	previous() - returns the previous element
	void	remove() - removes the current element
	void	set(E e) - sets the current element with the value provided

Example



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

	public class ExampleListIterator {
			public static void main(String a[]){
				Integer array[] = new Integer[]{10,5,6,3,91,200};
				List arrayList = new ArrayList<>(Arrays.asList(array));
				ListIterator itr= arrayList.listIterator();
				System.out.println("Using boolean  hasNext()");
				System.out.println("and E next()");

				while(itr.hasNext()){
					System.out.print(itr.next()+ " ");

				}

				System.out.println("\n");
				System.out.println("Using boolean hasPrevious()");
				System.out.println("and E previous()");
				while(itr.hasPrevious()){
					System.out.print(itr.previous()+ " ");

				}
				System.out.println("\n");
			}

		}

Output

Using boolean  hasNext()
and E next()
10 5 6 3 91 200

Using boolean hasPrevious()
and E previous()
200 91 3 6 5 10

Example 2



	import java.util.ArrayList;
	import java.util.Arrays;
	import java.util.List;
	import java.util.ListIterator;

	public class ExampleListIteratorAddRemove {
			public static void main(String a[]){
				Integer array[] = new Integer[]{10,5,6,3,91,200};
				List arrayList = new ArrayList<>(Arrays.asList(array));
				ListIterator itr= arrayList.listIterator();

				int index=0;

				System.out.println("Using void  add(E e)");
				while(itr.hasNext()){
					index++;
					itr.next();
					if(index==3)
					{
						itr.add(5000);
					}

				}
				System.out.println("After Adding an Element 5000 to the list...");

				System.out.println(arrayList);

				itr= arrayList.listIterator();
				System.out.println("\n");
				System.out.println("Using void remove()");
				index=0;
				while(itr.hasNext()){
					index++;
					itr.next();
					if(index==2)
					{
						itr.remove();//removes 5
					}

				}
				System.out.println("After removing the element at index 2 in the list...");

				System.out.println(arrayList);


			}

		}

Output


Using void  add(E e)
After Adding an Element 5000 to the list...
[10, 5, 6, 5000, 3, 91, 200]

Using void remove()
After removing the element at index 2 in the list...
[10, 6, 5000, 3, 91, 200]

<<Previous

Next >>







Learn about Stack Data Structure

Learn about Heap Data Structure

Learn about Operating System

Learn AVL Tree

Learn Djikstra's Algorithm