Java programming

when can this particular scenarioo be used Map<? extends K, ? extends V> m)... This is just an example taken frm the Hashmap class. I wanted the use in general and why ? is used.
[212 byte] By [nayak_pranamyaa] at [2007-10-3 4:39:41]
# 1
http://forum.java.sun.com/thread.jspa?threadID=767157&messageID=4373445#4373445
BIJ001a at 2007-7-14 22:43:37 > top of Java-index,Java Essentials,Java Programming...
# 2

This is a case of a "upper bounded" type. What "? extends K" means is that every object contained extends K.

The reason all this "boundedness" is needed is fairly complicated. It's because ther restriction on what you're going to get out of the collection aren't necessarilly the same as the restrictions on what you're allowed to put into it.

If I've got, say List<Dog> then I can assign it to a reference of

List<? extends Animal>

because a list of Dogs is always going to return objects assignable to animal.

On the other hand I can't safely add an object of type animal to a reference of

List<? extends Animal> because the list it points to might be restricted to Dog (or Cat) and the Animal might be the wrong species.

So LIst<? extends Animal>

means "a list from which everything returned will

be assignable to Animal." (of which a List<Dog> is an example.

On the other hand:

List<? super Dog>

means "A list to which it's safe to add Dogs". of which a List<Animal> would be an example.

malcolmmca at 2007-7-14 22:43:37 > top of Java-index,Java Essentials,Java Programming...