hashtable

Java: A “prime” number or a “power of two” as HashMap size?

戏子无情 提交于 2019-11-26 16:01:30
问题 Many books and tutorials say that the size of a hash table must be a prime to evenly distribute the keys in all the buckets. But Java's HashMap always uses a size that is a power of two. Shouldn't it be using a prime? What's better, a "prime" or a "power of two" as the hash table size? 回答1: Using a power of two effectively masks out top bits of the hash code. Thus a poor-quality hash function might perform particularly badly in this scenario. Java's HashMap mitigates this by mistrusting the

Iterating over and deleting from Hashtable in Java

倾然丶 夕夏残阳落幕 提交于 2019-11-26 15:45:53
问题 I have a Hashtable in Java and want to iterate over all the values in the table and delete a particular key-value pair while iterating. How may this be done? 回答1: You need to use an explicit java.util.Iterator to iterate over the Map 's entry set rather than being able to use the enhanced For-loop syntax available in Java 6. The following example iterates over a Map of Integer , String pairs, removing any entry whose Integer key is null or equals 0. Map<Integer, String> map = ... Iterator<Map

Is a Python dictionary an example of a hash table?

匆匆过客 提交于 2019-11-26 15:39:21
One of the basic data structures in Python is the dictionary, which allows one to record "keys" for looking up "values" of any type. Is this implemented internally as a hash table? If not, what is it? nosklo Yes, it is a hash mapping or hash table. You can read a description of python's dict implementation, as written by Tim Peters, here . That's why you can't use something 'not hashable' as a dict key, like a list: >>> a = {} >>> b = ['some', 'list'] >>> hash(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list objects are unhashable >>> a[b] = 'some'

Chained Hash Tables vs. Open-Addressed Hash Tables

时间秒杀一切 提交于 2019-11-26 15:19:27
问题 Can somebody explain the main differences between (advantages / disadvantages) the two implementations? For a library, what implementation is recommended? 回答1: Wikipedia's article on hash tables gives a distinctly better explanation and overview of different hash table schemes that people have used than I'm able to off the top of my head. In fact you're probably better off reading that article than asking the question here. :) That said... A chained hash table indexes into an array of

Hash tables in MATLAB

南笙酒味 提交于 2019-11-26 14:06:51
Does MATLAB have any support for hash tables? Some background I am working on a problem in Matlab that requires a scale-space representation of an image. To do this I create a 2-D Gaussian filter with variance sigma*s^k for k in some range., and then I use each one in turn to filter the image. Now, I want some sort of mapping from k to the filtered image. If k were always an integer, I'd simply create a 3D array such that: arr[k] = <image filtered with k-th guassian> However, k is not necessarily an integer, so I can't do this. What I thought of doing was keeping an array of k s such that: arr

Hash function for a string

走远了吗. 提交于 2019-11-26 13:57:38
问题 We are currently dealing with hash function in my class. Our instructor asked us to a hash function on the internet to compare to the two we have used in our code. The first one: int HashTable::hash (string word) // POST: the index of entry is returned { int sum = 0; for (int k = 0; k < word.length(); k++) sum = sum + int(word[k]); return sum % SIZE; } Second: int HashTable::hash (string word) { int seed = 131; unsigned long hash = 0; for(int i = 0; i < word.length(); i++) { hash = (hash *

Why hashmap lookup is O(1) i.e. constant time?

本秂侑毒 提交于 2019-11-26 13:07:05
问题 If we look from Java perspective then we can say that hashmap lookup takes constant time. But what about internal implementation? It still would have to search through particular bucket (for which key\'s hashcode matched) for different matching keys.Then why do we say that hashmap lookup takes constant time? Please explain. 回答1: Under the appropriate assumptions on the hash function being used, we can say that hash table lookups take expected O(1) time (assuming you're using a standard

Why Hashtable does not allow null keys or values?

烈酒焚心 提交于 2019-11-26 13:05:41
问题 As specified in JDK documentation, Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values. Why is this? 回答1: Hashtable is the older class, and its use is generally discouraged. Perhaps they saw the need for a null key, and more importantly - null values, and added it in the HashMap implementation. HashMap is newer, and has more advanced capabilities, which are basically just an improvement on the Hashtable functionality. When HashMap was

What&#39;s a correct and good way to implement __hash__()?

穿精又带淫゛_ 提交于 2019-11-26 12:41:32
What's a correct and good way to implement __hash__() ? I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries. As __hash__() returns an integer and is used for "binning" objects into hashtables I assume that the values of the returned integer should be uniformly distributed for common data (to minimize collisions). What's a good practice to get such values? Are collisions a problem? In my case I have a small class which acts as a container class holding some ints, some floats and a string. John Millikin An easy, correct way

What happens when a duplicate key is put into a HashMap?

泄露秘密 提交于 2019-11-26 12:00:44
If I pass the same key multiple times to HashMap ’s put method, what happens to the original value? And what if even the value repeats? I didn’t find any documentation on this. Case 1: Overwritten values for a key Map mymap = new HashMap(); mymap.put("1","one"); mymap.put("1","not one"); mymap.put("1","surely not one"); System.out.println(mymap.get("1")); We get surely not one . Case 2: Duplicate value Map mymap = new HashMap(); mymap.put("1","one"); mymap.put("1","not one"); mymap.put("1","surely not one"); // The following line was added: mymap.put("1","one"); System.out.println(mymap.get("1