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