I don't understand this code Map<E, Integer> d = new HashTable<E, Integer>(list.size()); : we create a new object but is it a map or a hashtable? what is the difference between the both of them? I thought that a map is just a way to put 2 element together like a key and its value (for exemple {3; Detroit})
is it a map or a hashtable?
Yes.
The static, compile time type of the reference is Map. As others have already pointed out, it's an interface. You can call all the methods on the Map interface and know that they'll obey the contract and behave as describe.
The dynamic, run time type of the object reference refers to is Hashtable. It implements all the methods in the Map interface in its own way.
The key idea is that the compile time type of a reference is separate from the run time type of the object on the heap that it points to.
Hashtable is a JDK 1.0 class that sticks around for compatibility reasons. It's been retrofitted to implement the Map interface, which was introduced later. You'd be well advised to choose another implementation, such as HashMap, depending on your requirements.
The last part of Hashtable contains the reason why it should not be used:
As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
This means that it is less efficient than HashMap for single-thread models and less efficient than ConcurrentHashMap for multi-threaded models.
Understanding how compile and run time types differ is crucial to understanding how object oriented polymorphism works. This is true for all OO languages: C++, Java, .NET, Python, etc.
Map is an interface. Hashtable is one of the classes that implements the Map interface.
See the Java Doc for the Map interface. Specifically the section that says all known implementing classes.
Any class that implements a Map provides a key->value data-structure. A Map being an interface defines the contract that all implementing classes must adhere to. By itself, a Map cannot be instantiated.
Note that while Hashtable should ideally have been named as HashTable following the java naming conventions, this is is a pre-historic class in Java which exists even before the standard java naming conventions came into existence. Therefore, it is still called Hashtable and not HashTable as wrongly mentioned in your question.
Map is an interface. HashTable is one implementation of that interface. There are several others, such as HashMap, SortedMap, etc. The interface defines the programming API; the implementation defines how that API is implemented. Different implementations may have different runtime performance characteristics.
With regard to interfaces vs. implementations, you may find my answer to Java - HashMap vs Map objects here helpful.
Related
What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?
In terms of Java, both the class HashMap and the class Dictionary are implementations of the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionary class and the Dictionary ADT.)
The Dictionary class has been marked as obsolete, so it's best not to use it.
This Stack Overflow post does a good job explaining the key differences:
Java hashmap vs hashtable
Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".
The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.
In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.
You are advised to use the HashMap though.
Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.
Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.
There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. here you can find the differences.
I know this question can be a little stupid but I just want to clear the doubt.
When going through Java tutorial for Collection ( http://docs.oracle.com/javase/tutorial/collections/index.html ), I didn't find any relevant information about both Vector and Hashtable. Both belong to Collection framework as Vector is implementation of List and Hashtable is implementation of Map. If it is so then why it is not in Sun tutorial? Where can I find Sun tutorial for Collection which contain good doc about both Vector and Hashtable and in depth knowledge about elements storing in List, Set and Map?
Because Vector and Hashtable are old, legacy collection classes. Don't use them.
Instead of Vector use ArrayList; instead of Hashtable use HashMap.
When Java 1.2 was released (very long ago), new collection classes were added to Java (the Collections Framework). Sun did not remove the old classes such as Vector and Hashtable because they wanted the new Java version to be backwards compatible. And now we still have those old classes.
One difference to be aware of is that Vector and Hashtable are synchronized, while ArrayList and HashMap are not. Most of the time you don't need synchronization; if you do, then you must take care to synchronize your ArrayList, and if you need a map, use ConcurrentHashMap instead of plain HashMap.
In general, Vector and Hashtable could be considered deprecated.
If you look at the online javadoc for Vector and Hashtable you'll see that they were the original implementation of ArrayList and HashMap, until the Collections framework came along, at which point they were retrofitted to implement interfaces from the Collections framework; this way, old classes that depended on those classes being there would not break. The only difference between them and their more common brethren is that they are synchronized.
In the vast majority of cases, synchronization isn't called for, so programmers will avoid the synchronization overhead and opt for regular ArrayLists and HashMaps. If a synchronized collection is desired there's always Collections.synchronized____() (or ConcurrentHashMap) that would work just fine too.
You probably don't need a tutorial for Vector and Hashtable because their behavior is already so similar to classes you're likely to be familiar with, and because they aren't used much any more. As for more info on List, Set, and Map, the online javadoc is a good place to start.
here are useful links for you
http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html
http://javarevisited.blogspot.com/2011/09/difference-vector-vs-arraylist-in-java.html
As mentioned by the JavaDoc of Vector:
As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.
it is kind of a legacy implementation of the List interface. The whole collection framework has been implemented to be by default not thread-safe. If you need thread safety, you may wrap any non tread-safe implementation by using the proper Collections.synchronizedXXX() methods, where XXX is List or Map or Set for example. The same applies for HashTable, which is by default synchronized as well. You should use HashMap instead and Collections.synchonizedMap() instead.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why should the interface for a Java class be prefered?
What is the benefit of polymorphism using Collection interface to create ArrayList object?
I see in many examples that when one creates an instance of some collection like TreMap, he uses it's base as type:
Map<String,String> theMap = new TreeMap<String,String>();
Why not to use
TreeMap<String,String> theMap = new TreeMap<String,String>();
Because it's considered a better practice to code to the interface (Map) instead of the implementation (TreeMap). This way you are free to switch to a different implementation (e.g. HashMap, if you later decide it is a better solution) with minimal code touch-points.
It's generally better to code to an interface, not an implementation, which allows an implementation change without having to change code that refers to it.
The only time it's reasonable to not do this is when code relies on characteristics of a specific implementation. For example, if the code requires the fast indexed list item access ArrayList provides, it might make sense to force a specific implementation.
In general it is better to program against Interfaces because it is easier, e. g., to switch from TreeMap (ordered) to HashMap (maybe faster).
If you use interfaces when possible, you only have to change one sourcecode part.
The reason is Liskov substitution principle. Directly quoted what was said.
Let q(x) be a property provable about objects x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.
Simple explanation
Map is an interface
And an interface is a contract
While using a Map we confirms that, we are using the contract of what a Map is supposed to do.
For example
doSomething(Map<String, String> map)
doSomething(TreeMap<String, String> map)
doSomething(Map<String, String> map) gives option to use any implementation which confirm the contract of Map allowing any of its subtype to work.
Whether doSomething(TreeMap<String, String> map) accepts any subtype of TreeMap. But TreeMap is not a contract so any subtype of TreeMap can violates its signature/behavior(methods actually). Thus making TreeMap less preferable compare to Map. Composition over inheritance can give u more idea about this.
This is one of java best pratices: program using interfaces if you can.
This helps to make you application more flexible because the method that receives this Map can also work on the other implementations of Map, but the method that received TreeMap can only use TreeMap and it's childrens.
So you get freedom to modify your method workings if you latter find that a HashMap would improve your application speed.
I recently ran across a set of code which instantiated local maps as following:
HashMap<String, Object> theMap = new HashMap<String, Object>();
Typically, when I've seen HashMaps used (and used them myself), the local variables are simply Map (the interface), rather than being tied to the specific implementation. Obviously this is required if the Map could potentially be instantiated as various Map types (e.g. accepting a parameter). However, in the case of something like the above where it's defined and instantiated at the same point, is there an underlying reason to only use the interface type, or is it simply style/convention?
(I originally misunderstood the question based on the title, but I've included both type and variable conventions as both are interesting.)
What's important is that it's a map: something you look things up in. The rest is an implementation detail.
I would suggest giving it a semantic name, e.g.
Map<String, Object> nameToSessionMap = ...
... that way when you read the code, you'll know what the keys and values are meant to be.
As for the type of the variable - again, I'd typically use the interface rather than the implementation partly because it indicates I'm not using any members which are specific to the type. I don't want to emphasize the implementation in the code, usually... it means when I do care about the implementation, I can make that more obvious.
Declaring the object as a Map will allow the compiler to protect you from calling methods which are specific to HashMap. This will allow you to substitute another Map implementation in the future without worrying about having method calls which do not exist in the Map interface.
In general people use mostly Map to make the least amount of assumptions on the implementation.
It cannot be that the Classname is used for the additional methods as only clone() is added by HashMap, which has fallen in disuse (for good reasons).
What could be is that the map needs to be Serializable for one reason or another, and the plain Map interface does not extend it, but HashMap does implement it.
Even in this case, it keeps it generic. Coding to interface ensures you are using a Map and not a specific implementation of it.
What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?
In terms of Java, both the class HashMap and the class Dictionary are implementations of the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionary class and the Dictionary ADT.)
The Dictionary class has been marked as obsolete, so it's best not to use it.
This Stack Overflow post does a good job explaining the key differences:
Java hashmap vs hashtable
Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".
The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.
In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.
You are advised to use the HashMap though.
Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.
Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.
There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. here you can find the differences.