hashtable

Why does HashMap require that the initial capacity be a power of two?

无人久伴 提交于 2019-12-17 06:28:11
问题 I was going through Java's HashMap source code when I saw the following //The default initial capacity - MUST be a power of two. static final int DEFAULT_INITIAL_CAPACITY = 16; My question is why does this requirement exists in the first place? I also see that the constructor which allows creating a HashMap with a custom capacity converts it into a power of two: int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; Why does the capacity always has to be a power of two? Also,

Time complexity of Hash table

耗尽温柔 提交于 2019-12-17 03:56:12
问题 I am confused about the time complexity of hash table many articles state that they are "amortized O(1)" not true order O(1) what does this mean in real applications. What is the average time complexity of the operations in a hash table, in actual implementation not in theory, and why are the operations not true O(1)? 回答1: It's impossible to know in advance how many collisions you will get with your hash function, as well as things like needing to resize. This can add an element of

Time complexity of Hash table

末鹿安然 提交于 2019-12-17 03:56:10
问题 I am confused about the time complexity of hash table many articles state that they are "amortized O(1)" not true order O(1) what does this mean in real applications. What is the average time complexity of the operations in a hash table, in actual implementation not in theory, and why are the operations not true O(1)? 回答1: It's impossible to know in advance how many collisions you will get with your hash function, as well as things like needing to resize. This can add an element of

What hashing function does Java use to implement Hashtable class?

 ̄綄美尐妖づ 提交于 2019-12-17 03:49:13
问题 From the book CLRS ("Introduction to Algorithms"), there are several hashing functions, such as mod, multiply, etc. What hashing function does Java use to map the keys to slots? I have seen there is a question here Hashing function used in Java Language. But it doesn't answer the question, and I think the marked answer for that question is wrong. It says that hashCode() let you do your own hashing function for Hashtable, but I think it is wrong. The integer returned by hashCode() is the real

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

冷暖自知 提交于 2019-12-17 02:25:42
问题 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",

How to do associative array/hashing in JavaScript

断了今生、忘了曾经 提交于 2019-12-17 00:19:33
问题 I need to store some statistics using JavaScript in a way like I'd do it in C#: Dictionary<string, int> statistics; statistics["Foo"] = 10; statistics["Goo"] = statistics["Goo"] + 1; statistics.Add("Zoo", 1); Is there an Hashtable or something like Dictionary<TKey, TValue> in JavaScript? How could I store values in such a way? 回答1: Use JavaScript objects as associative arrays. Associative Array: In simple words associative arrays use Strings instead of Integer numbers as index. Create an

Why two objects with the same hashcode are not necessarily equals? [duplicate]

拥有回忆 提交于 2019-12-16 18:05:22
问题 This question already has answers here : What issues should be considered when overriding equals and hashCode in Java? (11 answers) Closed 3 years ago . Currently I drilled into the JSE source code for fun. From some tutorials I found a principle two equals objects (i.e. as to object a and b a.equals(b) returns true) must have the same hashcode, on the other hand, two objects with the same hashcode are not necessarily equals . According to the HashTable source code of Java API (http:/

Behaviour of an hashtable using glib

 ̄綄美尐妖づ 提交于 2019-12-14 03:24:53
问题 I want to update the Volume to each @IP. So that for example after each 5 s I add V(i) of each @IP(i). Ok Now the hash table works fine it keeps updated after every T seconds . But the problem is that after a certain period I find that sometimes the same ip adress is repeated twice or even a lot of times within the hash table . So that when I close the process I find the same @IP repeated too many times. It is like there is a problem with the hash table or something like that. Here is the

HashMap : Dealing with Managed objects C++

*爱你&永不变心* 提交于 2019-12-14 03:15:18
问题 I guess it's kind of a stupid question but here is my problem : I want to have a hash_map<int, Object^> as an attribute of my object BigObject , which is written in managed C++. So I have to declare a pointer, hash_map<int, Object^>* hash because I cannot declare explicitely native object in managed code. How can I insert an object ? the hash_map[] won't work with a pointer, and I cannot make insert work (I cannot use a std::pair<int, Object^> because Object is managed... Thanks a lot 回答1:

C - Linked list of hash table keys

落爺英雄遲暮 提交于 2019-12-14 03:06:31
问题 I try to create a function that get in input an hash table and return a linked list of keys. This is the struct of a list node: struct hash_table_key_list_node_s { char *key; struct hash_table_key_list_node_s* next; }; typedef struct hash_table_key_list_node_s hash_table_key_list_node_t; typedef hash_table_key_list_node_t* hash_table_key_list_t; I don't understand why the list contains only one element but the hash table contains 330 element. This is the code of the function: hash_table_key