WIldcard inconsistency?

Why is this possible?

import java.util.*;

publicclass Test{

import java.util.*;

publicclass Test{

publicstaticvoid main(String[] args){

Map<String,Map><String,?>> mapOfMaps =

new HashMap<String,Map><String,?>>();

Map<String, Integer> stringToInteger =new HashMap<String,Integer>();

stringToInteger.put("bar", 350);

mapOfMaps.put("key1", stringToInteger);

Map<String,String> stringToString =new HashMap<String,String>();

stringToString.put("bar","foo" );

mapOfMaps.put("key2", stringToString );

}

}

This code works and effectively allows the storeage of both Map<String, Integer> and Map<String, String> types in parameterised type, turning it into a heterogeneous collection. Why does the compiler not prevent calling 'put' in the ususal way?

[1578 byte] By [blue_duea] at [2007-9-23]
# 1

Cause you can not assume the result of mapOfMaps.get("key1") to be neither String nor Integer, you are not into trouble.

While mapOfMaps itself is a heterogenious map, its values are not.

In general writing wildcard in type declaration says you have a homogenious type only for the class which type parameter is substituted by this wildcard

EugeneVigdorchika at 2007-7-11 > top of java,Core,Core APIs...
# 2
Ahh, I see.. Thanks for the reply!
blue_duea at 2007-7-11 > top of java,Core,Core APIs...