Is it more efficient to use the classroom, eg. Hashtable than the interface, eg Map?

advertisements

Will the compiler generate better code if it knows the actual class it will be working with vs the interface?

For example, I refer to my actual class like so:

Hashtable<String,String> foo() {
    Hashtable<String,String> table = new Hashtable<String,String>(100);
    ....
    return table;
}

...

    Hashtable<String,String> tbl = foo();

VS

Map<String,String> foo() {
    Map<String,String> table = new Hashtable<String,String>(100);
    ....
    return table;
}

...

    Map<String,String> tbl = foo();

Will the first form be more efficient?


OK, summarizing the answers now. I wish I could mark both Thomas and Tagir as correct, but I can't.

Thomas is correct in that the "correct" behavior is to use the abstract interface (Map) rather than the concrete implementation (Hashtable). This is the proper abstraction of the data and allows for the underlying implementation to be changed at will.

Tagir is correct in that exposing the concrete class allows certain compiler optimizations -- possibly highly significant optimizations. However, knowing if this will work or not requires knowledge of the compiler internals or benchmarking, and is not portable. It probably does not work for Android.

Finally, if you care about performance, don't use Hashtable; it's obsolete and clunky. If you really care about performance, consider using arrays instead.


In terms of runtime efficiency both should be equal as in both cases a Hashtable is used.

In terms of design using Map would be better in most cases, i.e. when it is irrelevant which implementation of Map is used. Generally you should use interfaces where possible so that you can replace the implementation, e.g. use a HashMap instead.

The difference between Hashtable and HashMap for example, would mainly be due to thread safety, i.e. Hashtable is synchronized and thus threadsafe while HashMap yields better performance due to the lack of synchronization. If you'd use Map in your interface you could also use ConcurrentHashMap without having to change the caller and get thread safety along with performance (although I'm not sure about how much difference there would be between ConcurrentHashMap an Hashtable).