First you specify the code, look through all the objects / objects in the collection A, But the last code says to go through the index from 0 to n, n=>size of A
In the normal case, both will work, and what to choose is usually based on which API for the loop you can use.
In your first code, the HashMap array is running or not populating. Say you have this data structure.
A = [1,2,3,NULL,NULL,4,NULL,5]
In your first case, you can only scroll through existing values. But in the latter case, you should skip all the elements.
Also, an unordered or clearly defined data structure, such as Maps, may have a method that ensures that you go through all the elements, but not in a clearly defined order.
source share