0

While using Iterator and For Each loop I found a major difference between the output. I expect it to be same but I don't know why this happen. Even I search about it but unable to any useful reason for the same.

public class EnhancedForLoopVsIterator {
      public static void main(String...args){

        Set s = new HashSet();
        s.add("abc");
        s.add(new String("abc"));
        s.add(null);

        Set s1 = new HashSet();
        s1.add("abc");
        s1.add(new String("abc"));
        s1.add(null);


        for(Iterator it = s.iterator();it.hasNext();){
              for(Iterator it1 = s1.iterator();it1.hasNext();){
                    System.out.println(it.next() + " & " + it1.next() );
              }
        }

        System.out.println("------------");

        for(Object obj: s){
              for(Object obj1: s1){
                    System.out.println(obj + " & " + obj1 );
              }
        }

      }
}

the output is as follows:

null & null
abc & abc
------------
null & null
null & abc
abc & null
abc & abc

3 Answers 3

2

it.next() will be called for every it1.hasNext(), this will proceed the outer iterator causing it.hasNext() to return false earlier than initially expected.

To achieve same output with iterator you'll need to do something like this:

for(Iterator it = s.iterator();it.hasNext();){
    Object n = it.next();
    for(Iterator it1 = s1.iterator();it1.hasNext();){
        System.out.println(n + " & " + it1.next() );
    }
}

Or with while-loop, which I find more commonly used:

Iterator it = s.iterator();
while(it.hasNext()){
    Object n = it.next();
    Iterator it1 = s1.iterator()
    while(it1.hasNext()){
        Object n2 = it1.next();
        System.out.println(n + " & " + n2);
    }
}

You can inline some of these parameters to make it look cleaner, I wrote it out just for clarity.

Sign up to request clarification or add additional context in comments.

Comments

0

Iterator is index based retrieval of object. In first loop pointer points to object at index 0 when you do it.next(), in the inner loop it again call it.next() and the pointer is moved to next index i.e 1 , y0u will get the object at index 1.

Comments

0

Here you are using

 for(Iterator it = s.iterator();it.hasNext();){
          for(Iterator it1 = s1.iterator();it1.hasNext();){
                System.out.println(it.next() + " & " + it1.next() );
          }
    }

one thing you need notice that whenever you use it.next() then it will take next value means to say we can call it as 3rd part of for loop.

So when it comes out of it1 iteration(2nd for loop) that time it will also comes out for 1st for loop.

BUt here

    for(Object obj: s){
          for(Object obj1: s1){
                System.out.println(obj + " & " + obj1 );
          }
    }

you are not changing the value of obj with obj1.

in other words you can understand that with iterator you are changing both iterator value at the same line but in foreach loop you are changing values at different places.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.