Series: Iterators

Java Collections Iterators explained in simple terms

Enumeration Interface Example

Enumeration Interface Example: Enumeration interface is from java.util package. It was introduced with JDK 1.0. It is used by many data structures especially with legacy classes. The DS of JDK 1.0 are known as legacy classes. They are Stack, Vector, Hashtable and Properties. Enumeration Interface Example import java.util.*; public class EnumerationDemo { public static void …

Enumeration Interface Example Read More »

Java Iterator Interface Example

Java Iterator Interface Example: JDK 1.0 Enumeration is replaced by Iterator interface in JDK 1.2. Being latest, it comes with many advantages over Enumeration. Like Enumeration, Java Iterator interface is also used for iterating the elements. Java Iterator Interface Example import java.util.*; public class IteratorDemo { public static void main(String args[]) { Vector vect1 = …

Java Iterator Interface Example Read More »

ListIterator Example Methods

We have seen earlier iterating Vector elements with Enumeration and Iterator. Now let us see with ListIterator. ListIterator is derived from Iterator interface and comes with more methods than Iterator. Always, subclass is richer than super class. Following is the class signature public interface ListIterator extends Iterator We know between two interfaces extends should be …

ListIterator Example Methods Read More »

Collections Enhanced new for loop

This is the second tutorial on the enhanced for loop on collections. Earlier one is with arrays – Arrays Enhanced for loop (foreach). Collections Enhanced new for loop Observe the following program. import java.util.ArrayList; public class ForEachCollections { public static void main(String args[]) { // enhanced for loop with generics Integer array list ArrayList al1 …

Collections Enhanced new for loop Read More »

Enumeration vs Iterator

After practicing Enumeration and Iterator with examples, let us summarize the differences. Both Enumeration and Iterator are used for iterating elements of a data structure. Iterator, being latest, comes with more features tabulated below. Enumeration Iterator Introduced with JDK 1.0 Introduced with JDK 1.2 Cannot remove any elements Can remove elements from the original DS …

Enumeration vs Iterator Read More »

Iterator vs ListIterator

You have seen the iteration of elements in the following examples earlier. 1. Using Enumeration interface. 2. Using Iterator interface. 3. Using ListIterator interface. Also you have seen the analogy of Enumeration vs Iterator. Differences between Enumeration, Iterator and ListIterator are very confusing for a Beginner. Understandability increases when placed at a single place. Towards …

Iterator vs ListIterator Read More »