hashtable

Sorting a HashMap based on Value then Key? [duplicate]

戏子无情 提交于 2019-11-29 04:33:33
Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? I have a HashMap of the type: HashMap<String, Integer> h = new HashMap<String, Integer>(); The HashMap contains a list of Strings and the Integer is a counter for the number of times that String has been found. What I would like to be able to do is sort the HashMap based on the Integers, then on the alphabetical order of the Strings. At the moment I am keeping a record of the largest occurrence of a word (variable named max) and displaying the values as follows: public void print(){ while(max > 0){ for (String key : h

Why push method is significantly slower than putting values via array indices in Javascript

只谈情不闲聊 提交于 2019-11-29 03:58:05
I pretty don't understand why this test : http://jsperf.com/push-method-vs-setting-via-key Shows that a.push(Math.random()); is over ten times slower than a[i] = Math.random(); Could you explain why this is the case ? What magic "push" do that make it so slow ? (or so slow compared to other valid method of doing that). EDIT NOTE: The push test is biased. I increase size of the array every iteration! Read carefully accepted answer! Bergi Could you explain why this is the case? Because your test is flawed. The push does always append to the existing a array making it much larger, while the

Reference as key in swift dictionary

≯℡__Kan透↙ 提交于 2019-11-29 03:45:01
Dictionary key requires Hashable conformance: class Test {} var dictionary = [Test: String]() // Type 'Test' dies not conform to protocol 'Hashable' class Test: NSObject {} var dictionary = [Test: String]() // Works How to get address of pure Swift class instance to use as hashValue ? Martin R For Swift 3 (Xcode 8 beta 6 or later), use ObjectIdentifier . class Test : Hashable { // Swift 2: var hashValue: Int { return unsafeAddressOf(self).hashValue } // Swift 3: var hashValue: Int { return ObjectIdentifier(self).hashValue } } func ==(lhs: Test, rhs: Test) -> Bool { return lhs === rhs } Then a

hash function for src dest ip + port

被刻印的时光 ゝ 提交于 2019-11-29 03:06:45
问题 So, I am looking at different hash functions to use for hashing a 4 tuple ip and port to identify flows. One I came across was ((size_t)(key.src.s_addr) * 59) ^ ((size_t)(key.dst.s_addr)) ^ ((size_t)(key.sport) << 16) ^ ((size_t)(key.dport)) ^ ((size_t)(key.proto)); Now for the life of me, I cannot explain the prime used (59). why not 31, and then why go mess it up by multiplying the sport by a power of 2. Is there a better hash function to be used for ip addresses ? 回答1: The prime number is

How to create a dictionary / hash table by iterating through a column?

别来无恙 提交于 2019-11-29 02:52:16
问题 I have a data frame of two columns: key and value and I would like to create a dictionary using the respective row of each column for each element of the dictionary / hash table. As far as I understand the typical way of using R dictionaries / hash tables is by doing something similar to this. labels.dic <- c("Id of the item and some other description" = "id") This works perfectly fine but when I try to do it using the values from the data frame (named lbls in the example) it does not work.

What's the point of a hash table?

帅比萌擦擦* 提交于 2019-11-29 02:17:46
问题 I don't have experience with hash tables outside of arrays/dictionaries in dynamic languages, so I recently found out that internally they're implemented by making a hash of the key and using that to store the value. What I don't understand is why aren't the values stored with the key (string, number, whatever) as the, well, key, instead of making a hash of it and storing that. 回答1: This is a near duplicate: Why do we use a hashcode in a hashtable instead of an index? Long story short, you

Hashtables and key order

好久不见. 提交于 2019-11-29 01:02:41
Is there a way to keep the order of keys in a hashtable as they were added? Like a push/pop mechanism. Example: $hashtable = @{} $hashtable.Add("Switzerland", "Bern") $hashtable.Add("Spain", "Madrid") $hashtable.Add("Italy", "Rome") $hashtable.Add("Germany", "Berlin") $hashtable I want to retain the order in which I've added the elements to the hashtable. Loïc MICHEL There is no built-in solution in PowerShell V1 / V2. You will want to use the .NET System.Collections.Specialized.OrderedDictionary : $order = New-Object System.Collections.Specialized.OrderedDictionary $order.Add("Switzerland",

convert HashTable to Dictionary in C#

我怕爱的太早我们不能终老 提交于 2019-11-28 22:27:55
how to convert a HashTable to Dictionary in C#? is it possible? for example if I have collection of objects in HashTable and if I want to convert it to a dictionary of objects with a specific type, how to do that? public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table) { return table .Cast<DictionaryEntry> () .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value); } var table = new Hashtable(); table.Add(1, "a"); table.Add(2, "b"); table.Add(3, "c"); var dict = table.Cast<DictionaryEntry>().ToDictionary(d => d.Key, d => d.Value); also you can create an extension method for

How much memory does a Hashtable use?

瘦欲@ 提交于 2019-11-28 20:52:38
In Java, if I create a Hashtable<K, V> and put N elements in it, how much memory will it occupy? If it's implementation dependent, what would be a good "guess"? Edit; Oh geez, I'm an idiot, I gave info for HashMap, not HashTable. However, after checking, the implementations are identical for memory purposes. This is dependent on your VM's internal memory setup (packing of items, 32 bit or 64 bit pointers, and word alignment/size) and is not specified by java. Basic info on estimating memory use can be found here . You can estimate it like so: On 32-bit VMs, a pointer is 4 bytes, on 64-bit VMs,

Cocoa: Dictionary with enum keys?

人走茶凉 提交于 2019-11-28 19:02:31
问题 I need to create a dictionary/hashmap where the Keys are enums Values are some subclass of NSObject NSDictionary won't work here (enums don't conform to NSCopying ). I could perhaps use a CFDictionaryRef here, but I'd like to know if is there any other way to achieve this. 回答1: Since enums are integers, you can wrap the enum in an NSNumber. When you add/retreive something to/from the map, you pass the enum to the NSNumber constructor... Assuming you've got an enum like... enum ETest { FOO,