java - Is it more efficient to use the class, e.g. Hashtable than the interface, e.g. Map? -


will compiler generate better code if knows actual class working vs interface?

for example, refer actual class 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 first form more efficient?


ok, summarizing answers now. wish mark both thomas , tagir correct, can't.

thomas correct in "correct" behavior use abstract interface (map) rather concrete implementation (hashtable). proper abstraction of data , allows underlying implementation changed @ will.

tagir correct in exposing concrete class allows compiler optimizations -- possibly highly significant optimizations. however, knowing if work or not requires knowledge of compiler internals or benchmarking, , not portable. not work android.

finally, if care performance, don't use hashtable; it's obsolete , clunky. if care performance, consider using arrays instead.

in terms of runtime efficiency both should equal in both cases hashtable used.

in terms of design using map better in cases, i.e. when irrelevant implementation of map used. should use interfaces possible can replace implementation, e.g. use hashmap instead.

the difference between hashtable , hashmap example, due thread safety, i.e. hashtable synchronized , threadsafe while hashmap yields better performance due lack of synchronization. if you'd use map in interface use concurrenthashmap without having change caller , thread safety along performance (although i'm not sure how difference there between concurrenthashmap hashtable).


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -