Using iterator

To iterate through each element present within a list, we use the iterator method. We need to create another object for this Iterator class, along with the String argument type:

        Iterator<String> i=hs.iterator();

Imagine that we have a set of elements and they are in order starting from zero, one, two, and so on. iterator goes through each element starting from zero and prints the element present at each value. We have created an object for iterator and we print the values as follows:

        System.out.println(i.next());        System.out.println(i.next());

The first instance of i.next() will print the values present at the zero index and the next i.next() instance prints the value at index one. If we have a set where we have around 100 ...

Get Hands-On Automation Testing with Java for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.