dictionary

.NET - I want to do this: Dim D as new Dictionary(of String, Array(OF MYOBJECT))

I'd like to create a Dictionary that is indexed by Strings: Dictionary(of String, ...) I'd like the Type after the comma to be an Array of MyObject's. If all I do is the following: Dim D as new Dictionary(of String, Array) I feel like I'm missing out on some performance very time I access a member: Dim Object1 as MyObject = MyDict...

.NET: Scalability of generic Dictionary

I'm using a Dictionary<> to store a bazillion items. Is it safe to assume that as long as the server's memory has enough space to accommodate these bazillion items that I'll get near O(1) retrieval of items from it? What should I know about using a generic Dictionary as huge cache when performance is important? EDIT: I shouldn't rely on...

Getting a KeyValuePair<> directly from a Dictionary<>

I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance A a (where dict.ContainsKey(a) is true). Is it possible to get the KeyValuePair containing a directly from the Dictionary? Or do I need to create a new KeyValuePair: new KeyValuePair<A, B>(a, dict[a])? ...

How to calculate freqency of all the words in a text document?

class CounterDict<TKey> { public Dictionary<TKey, int> _dict = new Dictionary<TKey, int>(); public void Add(TKey key) { if(_dict.ContainsKey(key)) _dict[key]++; else { _dict.Add(key, 1); } } } class Program { static void Main(string[] args) { string...

Why is there no Dictionary.TrimExcess()?

In .NET, there is a constructor for Dictionary<TKey, TValue> that takes one parameter, int capacity. This is the same as with many other collections such as List<T>, Queue<T>, and Stack<T>; furthermore, according to the MSDN documentation: The capacity of a Dictionary is the number of elements that can be added to the Dictionary befo...

C# Check Object Array For Duplicates

I have an array of Customer[] objects, and I want to use it to create a Dictionary<Customer, string>. What is the easiest way to examine the array for duplicates before I load the Dictionary? I want to avoid "ArgumentException: An item with the same key has already been added". Thanks. ...

Best practice for loading and using dictionary items?

Say I have some 10 "categories" that I need to reference in a web app. Currently I'm storing these categories in the DB and am retrieving them (category name and id) during pageload in my basepage, storing them in a hashtable and then using the hashtable to reference the categories. I realized that the DB call is being made during eac...

Custom Class used as key in Dictionary but key not found.

I have a class, show below, which is used as a key in a Dictionary<ValuesAandB, string> I'm having issues when trying to find any key within this dictionary, it never finds it at all. As you can see, I have overridden Equals and GetHashCode. To look for the key I'm using ValuesAandB key = new ValuesAandB(A,B); if (DictionaryName.Contai...

Remove Item in Dictionary based on Value

I have a Dictionary<string, string>. I need to look within that dictionary to see if a value exists based on input from somewhere else and if it exists remove it. ContainsValue just says true/false and not the index or key of that item. Help! Thanks EDIT: Just found this - what do you think? var key = (from k in dic where string.Co...

Cast Dictionary KeyCollection to String array

I have a Dictionary<int, string> which I want to take the Key collection into a CSV string. I planned to do: String.Join(",", myDic.Keys.ToArray().Cast<string[]>()); The cast is failing though. Thanks ...

Converting a single ordered list in python to a dictionary, pythonically

I can't seem to find an elegant way to start from t and result in s. >>>t = ['a',2,'b',3,'c',4] #magic >>>print s {'a': 2, 'c': 4, 'b': 3} Solutions I've come up with that seems less than elegant : s = dict() for i in xrange(0, len(t),2): s[t[i]]=t[i+1] # or something fancy with slices that I haven't figured out yet It's obviously ...

Add items to a Dictionary<int, List<int>>

So I am looping through some objects and initializing a Dictionary> object. So first I check if the key exists, if it does I will add to the List If it doesn't, I will create a new key and new List Is that the right logic? I will have to do a: new List<int>(); the first time I insert an item right? i.e.: if(myDic.ContainsKey(car....

Checking if my dictionary<int, list<int>> has an item in the list.

I am doing this right now: foreach(..) { if(myDic.ContainsKey(car.ID) && myDic[car.ID].Contains(car.MfgID)) { // do something } } I was wondering if I can perform the check, but check if it doesn't exist, and then just do a continue. How would I do that? using a || or &&? I cant' assume the key/value is there so I don't want it t...

Enumerating the inner dictionary using foreach

I have a dictionary of dictionaries, and I can't seem to figure out how to do a foreach loop on in the inner dictionary. My collection: Dictionary<int, Dictionary<int, User>> So far I have: foreach(User user in myDic[someKey]??) ...

Could it be a good idea to put Properties into Dictionaries?

Suppose I have a class with several properties that correspond to user-defined parameters, such as: bool StepUpHedge { get; set; } bool PullOnJump { get; set; } bool PullOnCross { get; set; } double MaxStockSpread { get; set; } double MaxHedgeSpread { get; set; } (These are just examples, not real code, and anyway what they mean isn't...

Getting the value from key in a dictionary that is nearest to a given number using JavaScript

I have a dictionary (keys are integers, values are float). I would now ask the dictionary for the value of the key that is > than the given number but < than the next greater key. Example: dict = {100: 0.0035, 150: 0.0024, 200: 0.0019}. i give 122, it should give me 0.0035 i give 333, it should give me 0.0019 i give 200, it should ...

Is the Objective-C dictionary an ordered container?

as the title, when I insert element to a dictionary in objective-c (in order like: k1, k2, k3), is there any guarantee that when I enumerate it like: for ( k in dictionary ){ // output the k - value } it would show in the same order? ...

NUnit: Dictionary Assert

I want a one liner, in NUnit, that asserts whether two dictionary are the same. i.e., I want a concise version of the below code: public static void DictionaryAssert<T, U>(Dictionary<T, U> dictionaryResult, Dictionary<T, U> expectedResult) { Assert.AreEqual(dictionaryResult.Count, expectedResult.Count); foreach (var aKey in expe...

Does Dictionary.Equals() have an implementation?

Hi all, I have a Dictionary which I am comparing to another Dictionary (variables typed as IDictionary). Doing d1.Equals(d2) yeilds false. Writing my own code below yields true. Both are System.Collections.Generic.Dictionary. Am I missing something or does Dictionary not have an Equals implementation that compares keys/values? priv...

Subclassing python's dict, override of __setitem__ doesn't retain new value

Hi, I'm subclassing dict, but ran into a problem with setitem where one assignment works, but another assignment does not. I've boiled it down to the following basic problem: class CustomDict(dict): def __setitem__(self, key, value): super(CustomDict, self).__setitem__(key, value) Test 1 fails: data = {"message":"foo"} CustomDict(...