Why is the get function of hashmap in Java get (Object key) instead of get (K key )?

Source: Internet
Author: User

Helping othersCodeAfter changing the bug, we found that a lot of bugs were caused by mismatch of the parameter types passed in by get or remove.

For example:

 
Map <short, string> M = new hashmap (); M. put (new short (short) 2), "2222"); system. out. println (M. get (2 ));

The above code output is null.

Generally, it is difficult to find that the int and short types passed in do not match, and the IDE and compiler do not prompt. Of course, some analysis tools can be used to check it out.

I am really confused. Some functions and parameters of the Java container are of the object type, such as the get and remove functions in hashmap AND THE contains functions in set.

Why are their types unclear? In this way, the compiler can check for Type Mismatch Errors.

Google, an engineer at Google gave the answer: http://smallwig.blogspot.com/2007/12/why-does-setcontains-take-object-not-e.html

For simplicity, use the set container as an example:

Define a simple s with only one simple contains function:

 
Class S <k> {public void contains (K) {system. Out. println ("s <k>, contains (K )");}}

Suppose we have a function and want to process the set of Foo classes:

 
Public void dosomereading (S <Foo> foos ){}

If we want to be able to simultaneously process the set of sub-classes of the foo class (such as subfoo), we should define it as follows:

 
Public void dosomereading (S <? Extends Foo> foos ){}

Everything looks good, but if we put all the Code together, we will find the tragedy:

 
Class S <k> {public void contains (K) {system. out. println ("s <k>, contains (K)") ;}} class Foo {} class subfoo extends Foo {} public void dosomereading (S <? Extends Foo> foos) {Foo F = new Foo (); subfoo = new subfoo (); Foos. contains (f); // here, eclipse will prompt an error. Here, foos will not be reported only when null is entered. contains (subfoo); // same error Foos. contains (null );}

In this case, the compiler does not work, which means it cannot work.

In the definition of the S <k> class, we define that the contains (K) function can only accept a parameter of a specific type.

But in the dosomereading function, the compiler cannot determine what type it is. Is it the foo type, the subfoo type, or the subsubfoo type?

The compiler does not know, so it only allows null parameters.

========================================================== ==============================

The explanation is still a bit depressing.

There is also another parsing that is related to the equals function. However, it is not very reliable. This can only be said to be an alternative application.

Http://stackoverflow.com/questions/857420/what-are-the-reasons-why-map-getobject-key-is-not-fully-generic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.