Why hashtable




















It can return null if the key is not found in the map or it can return null if the key is found and its value is null.

When a code expects null values, it usually checks if the key is present in the map so that it can know whether the key is not present or the key is present but value is null. Now this code breaks in a multi-threaded environment. Let's take a look at below code:. In the above code, let's say thread t1 calls the contains method and finds the key and it assumes that key is present and is ready for returning the value whether it is null or not.

Now before it calls map. Now t1 resumes and returns null. However as per the code, the correct answer for t1 is KeyNotFoundException because the key has been removed. But still it returns the null and thus the expected behavior is broken.

Now, for a regular HashMap, it is assumed, that it is going to get called by a single thread, hence there is no possibility of key getting removed in the middle of "contains" check and "get". So HashMap can tolerate null values. However for Hashtable and ConcurrentHashMap, the expectations are clear that multiple threads are going to act on the data. Hence they cannot afford to allow null values and give out incorrect answer. Same logic goes for keys.

That is correct, it can happen. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress except through the iterator's own remove operation , the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.

It does not support the add or addAll operations. If the map is modified while an iteration over the set is in progress except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator the results of the iteration are undefined.

The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress except through the iterator's own remove operation , the results of the iteration are undefined.

The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy. Object java. Any non- null object can be used as a key or as a value. Constructor Summary Constructors Constructor and Description Hashtable Constructs a new, empty hashtable with a default initial capacity 11 and load factor 0.

Constructs a new, empty hashtable with the specified initial capacity and default load factor 0. Constructs a new, empty hashtable with the specified initial capacity and the specified load factor. Returns a Set view of the mappings contained in this map. Compares the specified Object with this Map for equality, as per the definition in the Map interface. Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. Returns the hash code value for this Map as per the definition in the Map interface.

Returns a Set view of the keys contained in this map. Maps the specified key to the specified value in this hashtable. Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently.

Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters " , " comma and space. Returns a Collection view of the values contained in this map.

Constructor Detail Hashtable public Hashtable int initialCapacity, float loadFactor Constructs a new, empty hashtable with the specified initial capacity and the specified load factor. Constructs a new, empty hashtable with a default initial capacity 11 and load factor 0. Constructs a new hashtable with the same mappings as the given Map. Returns an enumeration of the values in this hashtable. Tests if some key maps into the specified value in this hashtable.

Returns true if this hashtable maps one or more keys to this value. A hashtable is used to store a set of values and their keys in a for some amount of time constant number of spots. This would make a hashtable of blocks often an array , each having a list 10 elements deep. So if you were to search for , it would immediately know to search in the table entry for , then start comparing to find the exact match.

Granted, this isn't much better than just using an array of elements, but it's just to demonstrate. Hashtables are very useful for when you don't know exactly how many elements you'll have, but there will be a good number fewer collisions on the hash function than your total number of elements.

You may have empty spots in your table, but ideally a majority of them will have some data. The main advantage of using a hash for the purpose of finding items in the table, as opposed to using the original key of the key-value pair which BTW, it typically stored in the table as well, since the hash is not reversible , is that..

This O 1 performance gets a bit eroded when considering the extra time to dealing with collisions and such, but on the whole the hash table is very fast for storing and retrieving items, as opposed to a system based solely on the [original] key value, which would then typically be O log N , with for example a binary tree although such tree is more efficient, space-wise. Also consider speed.

If your key is a string and your values are stored in an array, your hash can access any element in 'near' constant time.

Compare that to searching for the string and its value. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What's the point of a hash table? Ask Question. Asked 11 years, 9 months ago. Active 5 years, 2 months ago. Viewed 27k times. Improve this question.

Javier Javier 4, 6 6 gold badges 33 33 silver badges 45 45 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Community Bot 1 1 1 silver badge. How it works? See the below diagram it clearly explains.

Advantages: In a well-dimensioned hash table, the average cost for each lookup is independent of the number of elements stored in the table. Many hash table designs also allow arbitrary insertions and deletions of key-value pairs. Disadvantages: The hash tables are not effective when the number of entries is very small.

Uses: They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches and sets. Durai Amuthan.



0コメント

  • 1000 / 1000