Hello and good day to you all!
I was confused about some
Map theory and was hoping someone could help me sort it out.
I have been reading the older 1.4 version of Ivor Horton�s Beginning
Java, and am on the section about HashMaps. Now, he talks about one can get a Set of entries from the HashMap using the entrySet() method.
Now, here�s the rub. The contents of this set are objects of type Map.Entry. But here is what threw me:
�The key/object pairs are of type Map.Entry because Entry is an
interface declared within the Map interface.�
Interface?! But Horton says in Chapter 06:
�The declaration of the class as abstract is mandatory when you don't implement all of the methods that are declared in an interface.�
It seems that Map.Entry is abstract, since it appears that the five methods contained in the class (
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.Entry.html) have not been implemented. Or have they�
In Horton�s HashMap section, Horton refers to the Set elements as Map.Entry
objects.
In addition, look at this code from Oracle (
http://www.oracle.com/technology/pub/articles/maps1.html) :
Iterator keyValuePairs1 = aMap.entrySet().iterator();
for (int i = 0; i < mapsize; i++)
{
Map.Entry entry = (Map.Entry) keyValuePairs1.next();
Object key = entry.getKey();
Object value = entry.getValue();
...
}
Again, we use an interface to create an object? So, here is the million dollar question:
** Did HashMap likely implement the Map.Entry interface?
My thinking goes like this: Map is an interface, which also nests another interface within it: Map.Entry. However, Map.Entry is static, so we don't need to implement Map.Entry with a Map implementation.
Also, a corollary:
** Could the five methods be overridden by either extending HashMap or by creating a class with a HashMap member, thereby potentially controlling access to the HashMap?
Much obliged!
-gabe