Set key1 = map.keySet();
Iterator it1 = key1.iterator();
int cnt=0;
while (it1.hasNext()) {
cnt++;
}
What are the chances that this code will result
Basically itl.hasNext() always return a boolean value based on the availability of next value to process in the collection. Say pointer is at some position x, hasNext() returns true if there exists some element next to position x, i mean x+1 element exists,
So you must use itl.next(), which returns the current element in the collection and moves the pointer ahead by 1. so according to our previous example, next() returns current object a position x, and moves the pointer to next poiton.
However to get the number of elements from a collection you could use
collection_object.size()