May you explain the following statement about the code:
Collection<String> stringCollection = new HashSet<String>();
stringCollection.add(new String ("bye"));
stringCollection.add(new String ("hi"));
stringCollection.add(new String ("bye again"));
for( Iterator<String> iter=stringCollection.iterator();
iter.hasNext();){
String str=iter.next();
if(str.equals("hi"))
iter.remove();
}
for (String str: stringCollection){
if(str.equals("hi"))
stringCollection.remove("hi");
}
System.out.println(stringCollection.size());
If we change the order of both loops, then the code will run without errors and print 2: Wrong there is a runtime error, But why it seems correct?