hashtable

Best way to initialize a HashMap

断了今生、忘了曾经 提交于 2020-01-01 15:30:26
问题 I usually do e.g. HashMap<String,String> dictionary = new HashMap<String,String>(); I started to think about it, and as far as I know a HashMap is implemented under the hood via a hash table. The objects are stored in the table using a hash to find where they should be stored in the table. Does the fact that I do not set a size on the construction of the dictionary makes the performace decrease? I.e. what would be the size of the hash table during construction? Would it need to allocate new

Fast C++ container like the C# HashSet<T> and Dictionary<K,V>?

倖福魔咒の 提交于 2020-01-01 05:25:26
问题 I've used HashSet and Dictionary a lot in C#, and found them very fast... I've tried using std::map and std::hash_map and am finding them very slow in comparision. Does this sound like expected behaviour? Is there something I might be doing wrong in my use of std::hash_map? Or, is there a better C++ Hash container out there? I'm hashing int32s, usually around 100,000 of them. Update: I created a repro in C# and C++. It runs two trials, they take 19ms and 13ms in C#, and about 11,000ms in C++.

What is the easiest way to sort maps according to values in Java?

∥☆過路亽.° 提交于 2020-01-01 03:19:08
问题 I want my hash to sort in descending order according to the values. How do I do that in Java? 回答1: A HashMap (and its legacy predecesor Hashtable) is by nature unordered. Even if you sort it, it will remain unordered. If you want to maintain insertion order, then use LinkedHashMap instead. If you want an automatic sort on keys , regardless of insertion order, then use SortedMap instead. If you want to sort a Map on values , then you basically need to put the key/value pairs in another kind of

how to find and return objects in java hashset

点点圈 提交于 2019-12-31 21:30:11
问题 According to the HashSet javadoc, HashSet.contains only returns a boolean. How can I "find" an object in a hashSet and modify it (it's not a primitive data type)? I see that HashTable has a get() method, but I would prefer to use the set. 回答1: You can remove an element and add a different one. Modifying an object while it is in a hash set is a recipe for disaster (if the modification changes the hash value or equality behavior). 回答2: To quote the source of the stock Sun java.util.HashSet:

Map list onto dictionary

删除回忆录丶 提交于 2019-12-31 17:54:34
问题 Is there a way to map a list onto a dictionary? What I want to do is give it a function that will return the name of a key, and the value will be the original value. For example; somefunction(lambda a: a[0], ["hello", "world"]) => {"h":"hello", "w":"world"} (This isn't a specific example that I want to do, I want a generic function like map() that can do this) 回答1: I don't think a standard function exists that does exactly that, but it's very easy to construct one using the dict builtin and a

Efficiently picking a random element from a chained hash table?

一笑奈何 提交于 2019-12-31 08:49:47
问题 Just for practice (and not as a homework assignment) I have been trying to solve this problem (CLRS, 3rd edition, exercise 11.2-6): Suppose we have stored n keys in a hash table of size m, with collisions resolved by chaining, and that we know the length of each chain, including the length L of the longest chain. Describe a procedure that selects a key uniformly at random from among the keys in the hash table and returns it in expected time O(L * (1 + m/n)). What I thought so far is that the

Concrete examples of using binary search trees?

…衆ロ難τιáo~ 提交于 2019-12-31 08:13:09
问题 I understand how binary search trees are implemented, but I am not sure what are the advantages of using it over the hash tables that most programming languages have built into their standard libraries. Could someone please provide examples of real-world problems solvable with binary search trees? 回答1: There are a few theoretical advantages of binary search trees over hash tables: They store their elements in sorted order . This means that if you want to store the container in a way where you

Collection was modified; enumeration operation may not execute. when update values of hashtable

守給你的承諾、 提交于 2019-12-31 06:28:20
问题 this code throw exception while i am trying to update value ,first value only updated and then throw the exception "Collection was modified; enumeration operation may not execute." !!!! Hashtable hh = new Hashtable(); hh.Add("val 1",null); hh.Add("val 2", null); foreach (string dd in hh.Keys) { hh[dd] = "some_value"; // MessageBox.Show(dd.Value.ToString()); } i need to update empty values in hashtables or any equivalent structure that has [key,value]?? 回答1: You need to make copy of hh.Keys,

Why won't a Hashtable return true for “ContainsKey” for a key of type byte[] in C#?

让人想犯罪 __ 提交于 2019-12-31 03:15:23
问题 Consider the following code: byte[] bytes = new byte[] { 1, 2, 5, 0, 6 }; byte[] another = new byte[] { 1, 2, 5, 0, 6 }; Hashtable ht = new Hashtable(); ht.Add(bytes, "hi"); Assert.IsTrue(ht.ContainsKey(another)); Why does this assertion fail? Being an array of a primitive type shouldn't use using the object reference, should it? So why would it return false? Is there anything I can do to make this hashtable work? 回答1: Here's a sample implementation: class Program { static void Main(string[]

Why won't a Hashtable return true for “ContainsKey” for a key of type byte[] in C#?

家住魔仙堡 提交于 2019-12-31 03:15:07
问题 Consider the following code: byte[] bytes = new byte[] { 1, 2, 5, 0, 6 }; byte[] another = new byte[] { 1, 2, 5, 0, 6 }; Hashtable ht = new Hashtable(); ht.Add(bytes, "hi"); Assert.IsTrue(ht.ContainsKey(another)); Why does this assertion fail? Being an array of a primitive type shouldn't use using the object reference, should it? So why would it return false? Is there anything I can do to make this hashtable work? 回答1: Here's a sample implementation: class Program { static void Main(string[]