I have following two classes of codes :
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class TestSet {
public static void main(String[] args) {
        Set<Integer> set = new HashSet<>();
        set.add(90);
        set.add(10);
        set.add(20);
        set.add(30);
        System.out.println(set);
        Iterator<Integer> itr = set.iterator();
        while(itr.hasNext()){
            Integer ob = itr.next();
            if(ob.equals(10)){
                set.add(11);
            }
        }
        System.out.println(set);
    }
}
output of above code is
[20, 90, 10, 30]
Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextNode(HashMap.java:1442)
    at java.util.HashMap$KeyIterator.next(HashMap.java:1466)
    at com.collection.TestSet.main(TestSet.java:18)
and another class
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class TestIntegerSet {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("90");
        set.add("10");
        set.add("20");
        set.add("30");
        System.out.println(set);
        Iterator<String> itr = set.iterator();
        while(itr.hasNext()){
            String ob = itr.next();
            if(ob.equals("10")){
                set.add("11");
            }
          }
        System.out.println(set);
    }
}
while output of above code is
[90, 30, 20, 10]
[11, 90, 30, 20, 10]
I am not able to understand why there is strange behavior? Or I am doing something wrong?
I am trying to iterate using iterator but why its throwing concurrent modification exception for integer, while its showing proper value for String.
 
     
     
    