Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashTable Issue

I have an interesting question which entails the use of Hashtables; I'm developing for S40 Nokia's (with compliance level 1.4)

How I expect the Hashtable to work:

Hashtable table = new Hashtable();
table.put(1, "Hello World");

However I get the error:

The method put(Object, Object) in the type Hashtable is not applicable for the arguments (int, String)

However when I create an object reference and pass the reference, it works fine! Why?!

Working example:

Hashtable table = new Hashtable();
Integer test = new Integer(1);
table.put(test, "Hello World");

Any explanations would be great!

like image 733
dan983 Avatar asked Jul 14 '14 11:07

dan983


People also ask

Why wouldn't you use a hash table?

There are some operations which are not efficiently supported by hash tables, such as iterating over all the elements whose keys are within a certain range, finding the element with the largest key or smallest key, and so on. The O(n) complexity is on average.


3 Answers

In my answer I suppose that your actual code was in fact the following:

Hashtable table = new Hashtable();
table.put(1, "Hello World");

That's the code which causes the error you have described, i.e.

The method put(Object, Object) in the type Hashtable is not applicable for the arguments (int, String)

The reason is this:

  1. Java 1.4 does not support generics, so the Hashtable simply works with Objects (both as keys as well as values)

  2. Java 1.4 does not support autoboxing, so the code table.put(1, "Hello World") is not automatically autoboxed to table.put(Integer.valueOf(1), "Hello World"). Hence you are trying to call table.put(int, String) which is not compatible with Hashtable.put(Object, Object).

Voila.

If you used Java 1.5+, the call would be autoboxed to table.put(Integer, String)

BTW, do not use new Integer(1), always prefer the static factory method Integer.valueOf(1). You may avoid unnecessary creation of redundant classes. This is what the autoboxing is compiled into. See this: Static factory methods vs Instance (normal) constructors?

like image 166
Honza Zidek Avatar answered Oct 26 '22 13:10

Honza Zidek


From the error message you mentioned,

The method put(Object, Object) in the type Hashtable is not applicable for the arguments (int, String)

It is clear that your compiler treats the Integer object as a primitive value just after it is initialized. Namely it applies unboxing immediately. This might have been done for optimiziation in mobile platforms, if I can find a reference for it, I'll update my answer.

like image 27
Juvanis Avatar answered Oct 26 '22 13:10

Juvanis


Problem with your code is that, as you mentioned, is 1.4 compliance, which makes me think you're compiling for it to be 1.4 compatible. Boxing / unboxing is a feature added in 1.5.

Just for you to confirm what I mean: try compiling your code with javac --source 1.5 --target 1.5, it will compile fine, but try the same with javac --source 1.4 --target 1.4 then it will complain

like image 37
morgano Avatar answered Oct 26 '22 13:10

morgano