Various ways to iterate through Vector – 5 ways

In this article, we will discuss various ways to iterate through Vector objects

 

Various ways to iterate through Vector

  1. for-loop (regular)
  2. Enhanced for-loop (introduced in Java 1.5 version)
  3. Iterating using Iterator of Collection interface (Java 1.2 version)
  4. Iterating using ListIterator of List interface (Java 1.2 version)
  5. Enumeration interface (since Java 1.0 version)

 

Example: Various ways to iterate through Vector

DifferentWaysToIterateVector.java

package in.bench.resources.java.collections;

import java.util.Vector;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;

public class DifferentWaysToIterateVector {

	public static void main(String[] args) {

		// creating Vector object of type String
		Vector<String> v = new Vector<String>();

		// adding elements to Vector object
		v.add("Sundar Pichai");
		v.add("Satya Nadella");  
		v.add("Shiv Nadar");  
		v.add("Shantanu Narayen");
		v.add("Sundar Pichai"); // duplicate object
		v.add("Francisco D’Souza");
		v.add(null); // null is allowed


		// Way 1: Iterating using for-loop
		System.out.println("Way 1: Iterating using for-loop\n");
		for(int index = 0; index < v.size(); index++) {
			System.out.println(v.get(index));
		}

		// Way 2: Iterating using enhanced for-loop
		System.out.println("\n\nWay 2: Iterating using "
				+ "enhanced for-loop\n");
		for(String str : v) {
			System.out.println(str);
		}

		// Way 3: Iterating using Iterator of Collection interface
		System.out.println("\n\nWay 3: Iterating using "
				+ "Iterator of Collection interface\n");
		Iterator<String> itr = v.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}

		// Way 4: Iterating using ListIterator of List interface
		System.out.println("\n\nWay 4.A: Iterating using ListIterator of "
				+ "List interface in FORWARD direction\n");
		ListIterator<String> lstItr = v.listIterator();
		while(lstItr.hasNext()) {
			System.out.println(lstItr.next());
		}

		System.out.println("\n\nWay 4.B: Iterating using ListIterator of "
				+ "List interface in BACKWARD direction\n");
		while(lstItr.hasPrevious()) {
			System.out.println(lstItr.previous());
		}

		// Way 5: Iterating using Enumeration interface
		System.out.println("\n\nWay 5: Iterating using "
				+ "Enumeration interface\n");
		Enumeration<String> e = v.elements();
		while(e.hasMoreElements()) {
			System.out.println(e.nextElement());
		}
	}
}

Output:

Way 1: Iterating using for-loop

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null


Way 2: Iterating using enhanced for-loop

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null


Way 3: Iterating using Iterator of Collection interface

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null


Way 4.A: Iterating using ListIterator of List interface in FORWARD dir

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null


Way 4.B: Iterating using ListIterator of List interface in BACKWARD dir

null
Francisco D’Souza
Sundar Pichai
Shantanu Narayen
Shiv Nadar
Satya Nadella
Sundar Pichai


Way 5: Iterating using Enumeration interface

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null

 

From above example, Vector

  • allows duplicate elements
  • null object is allowed
  • while iterating insertion order is maintained

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html
https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html
https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html

 

Happy Coding !!
Happy Learning !!

Conversion of ArrayList into HashSet to remove duplicate elements
How to reverse LinkedHashSet contents