Map.get(Object o) vs Map.get(K k) ?
Hi All,
I hace a question about Map (or may be TreeMap as it comes into my notice whle using TreeMap). I posted it on Collections related forum and got referred here
[url]http://forum.java.sun.com/thread.jspa?threadID=684755[/url]
I was wondering why some methods of Map<k,V> specify Object as type for keys used and not the given type K. problem is that following code will throw a ClassCastException
String key="146564";
TreeMap<String, String> map =new TreeMap<String, String>();
map.put(key,key);/*If you uncomment this line you will get Exception*/
map.containsValue(new Long(key));/*can compile */
map.containsKey(new Long(146564));/*can compile */
String v = map.get("1");
//map.put(new Long(1), "");/*Can't compile*/
Exception in thread"main" java.lang.ClassCastException: java.lang.String
at java.lang.Long.compareTo(Long.java:34)
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.getEntry(TreeMap.java:347)
at java.util.TreeMap.containsKey(TreeMap.java:204)
atTestClass.main(TestClass.java:27)
It is suggested that since I am passing a Long to map.containsKey(), where as Map was declared as Map<String,String> I should expect to get such nasty runtime exceptions. My thinking is 'generics' are specifically designed to avoid such situations.
Can any one let me know why 'generics' are implemented such a way that is not orthognal accross various interfaces? i.e. why is it that I need to remember what instance of a Map was created with key/value pairs of Type A/B and which one with C/D, however if I am using Collection, it will be type checked at comple time? I would either expect to remember it always or to expet it to get type checked at compile time always.

