To sum up, i.next()
is returning a Map.Entry
but we are storing it in an Object
Class reference variable because java allows super class reference variable to hold the sub class object and Object
class is super class to all.
Why typecasting? because we can't invoke sub class methods on a super class variable.
If you want the iterator to return Map.Entry without type casting, declare Iterator as Map.Entry
Iterator like this Iterator<Map.Entry> i
. Happy learning!