'm not able to understand why next() in the code is calling getName() of Zinger on executing the code .
That's because it isn't calling that method. And you haven't posted anything that shows that it is. This is the tail end of the output
. . .
7is i of 12568800currently c is :8
this is Zinger9
this is Zinger8
this is Zinger5
this is Zinger6
this is Zinger7
this is widout comparatorZinger9
I assume that the lines such as 'this is Zinger9' are where you think that 'getName' is being called by 'itp.next()'.
But it is actually YOUR code that creates the collection entries that is calling 'getName'
You are calling 'getName' and using that as the value (e.g. 'Zinger9') of the entry. My 'guess' is that you meant to use 'z.getName' as the KEY and to use 'z' itself (the Zinger instance) as the value. Then you would probably want to create a collection of the 'keys' and iterate over them.
But instead you are creating a collection of those 'string' values
Collection cp =tp.values();
Each of those values is a string. And then you create an iterator over that collection of 'strings':
And then in a loop you use that iterator to execute this code:
System.out.println("this is "+itp.next());
So 'next' is merely getting the next string from your collection of strings. It isn't referencing any Zinger instance at all.
I suggest that you begin using a GUI such as NetBeans and examine the variables you are creating as you step through the code using the debugger. If you do that you can see what values those variables and collections contain.