Set key1 = map.keySet(); Iterator it1 = key1.iterator(); int cnt=0; while (it1.hasNext()) { cnt++; }
What are the chances that this code will result
The problem is, that you don't call it1.next() in the loop, so it1.hasNext() is always true. In other words, you never move to the next item.
it1.next()
it1.hasNext()
BTW, you don't need iterator for what you want to archieve, try
int cnt=map.size();