The ip2nation table stores ip values like such:

IP      COUNTRY
687865856   za
689963008   eg


How does this translate into an actual IP address block range?


Question: How remove works for ArrayList while iterating using for-each loop?
Answer: The list has a variable called modCount. Whenever you call remove (or perform other structural modifications), it increments the modCount.

At the start of iteration, the iterator makes a note of the modCount, saving it as expectedModCount. When each item is read from the iterator, the iterator checks if the modCount equals the expected value, and throws an exception if it doesn't.

Usually, this will successfully cause the exception to be thrown if the list is unsafely modified during iteration. However, it's not sufficient in this case when the if statement is enabled. After your code has read "World", that item is removed, and so the list now contains ["Hello", Good Evening"]. The iterator is still at position 1 (which now contains "Good Evening") and when it tries to read the next item, it finds it has now reached the end of the list, so it doesn't bother to check the modCount. Hence, no exception.

Note the caveat in the ConcurrentModificationException documentation: "It is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw ConcurrentModificationException on a best-effort basis."

The code is still wrong though, even if it doesn't happen to throw the exception in this case. To remove an element while iterating, you must use the iterator's own remove method:

for (Iterator&lt;String&gt; it = list1.iterator(); it.hasNext();) {
    String s = it.next();
    if (s.equals("World")) {
        it.remove();
    }
}


That way, the iterator knows that the list has changed and can still iterate correctly.

Alternatively, you can iterate from a copy of the list:

for (String s : new ArrayList&lt;&gt;(list1)) {
    if (s.equals("World")) {
        list1.remove(...);
    }
}


Although, in this simple case, you don't even need to do that; you can just write:

list1.remove("World");