-
HashMap vs HashTable
Hello, I am learning java right now. While learning it, I am not able to get the difference between HashMap and HashTable. If I look at the description of both, it looks like similar. So, if anyone knows actual difference then please reply me about the same.
-
Re: HashMap vs HashTable
If you look at the use of the both then you will come to know that they will give you key-value from which you can make use of the data. The Hashtable is the class from the java Collections. And the HashMap is part of Framework of the Collections. If you consider the main difference between then then you will come to know that HashTable is having synchronized access and HashMap is not having synchronized access.
-
Re: HashMap vs HashTable
HashMap and HashTable is having difference that iterator in the HashMap is fail-safe but if you are using enumerator in your hashtable then it may possible that it will fail while using. If you change the map while iterating then you can able to know this. So, just try it. If you are using HashMap then you can provide the null values in that. But if you are using HashTable then it may not possible that adding null values in that.
-
Re: HashMap vs HashTable
The HashMap class and Hashtable is quiet similar if you look at the features as well as working of both. But, they are different in there Synchronization strategy. As one will provide it, and other would not. If you make use of the hashmap then it will provide you the facility of the using null values whereas the HashTable won't allow you to use the null value in it. And if you are using the hashmap then it will not provide you the ability of providing constant map, but the HastTable will allow you this facility.
-
Re: HashMap vs HashTable
Take a look at the use of both HashMap and HashTable in coding:
HashTable:
Code:
Hashtable<Integer,String> testing = new Hashtable<Integer,String>();
testing.put(1, "Lahore");
testing.put(2, "Karachi");
testing.put(3, null);
System.out.println(testing.get(1));
System.out.println(testing.get(2));
System.out.println(testing.get(3));
HashMap:
Code:
HashMap<Integer,String> testing = new HashMap<Integer,String>();
testing.put(1, "Keys");
testing.put(2, null);
-
Re: HashMap vs HashTable
HashTable and HashMap both are similar if you look at overall but they are having difference in there behavior.
Hashtable:
- It is a Data Structure.
- It will store your value of Key pairs.
- It will not provide you use of the null values.
- It is synchronized.
- It is having slower performance.
HashTable:
- It accepts key value pair.
- It will provide you the facility of using null value.
- It is Not Synchronized.
- It is having faster performance.
-
Re: HashMap vs HashTable
1. The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).
2. HashMap does not guarantee that the order of the map will remain constant over time.
3. HashMap is non synchronized whereas Hashtable is synchronized.
4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't
to read more about Hashtable vs HashMap see here.
Page generated in 1,714,095,506.19590 seconds with 10 queries