collections

How to sort NameValueCollection using a key in C#?

橙三吉。 提交于 2020-01-03 11:45:17
问题 I've written following code and it works too - but I wanted to know whether their is better way than this : NameValueCollection optionInfoList = ..... ; if (aSorting) { optionInfoListSorted = new nameValueCollection(); String[] sortedKeys = optionInfoList.AllKeys; Array.Sort(sortedKeys); foreach (String key in sortedKeys) optionInfoListSorted.Add(key, optionInfoList[key]); return optionInfoListSorted; } 回答1: Use a SortedDictionary instead. 回答2: Perhaps you could use a different kind of list,

From arrayList to long[]

这一生的挚爱 提交于 2020-01-03 11:04:04
问题 I am doing a method that needs to return a long[]. It looks like this: public long[] parseString(String input) input are strings like: 1, 3, 4 10, 30, 40, 50 Inside parseString I use a regex to get all numbers and add them to an ArrayList as I can't know how many oconcurrences it will find. At the end I create a long[] with the size of the arrayList and do a for each to add it to the long[] var. Another way would be: First count every occurrence with a while ( matcher.find() ) size++; and

How to get the closest item to my key from a SortedDictionary?

萝らか妹 提交于 2020-01-03 09:29:09
问题 Currently I'm using a binary search over a SortedList<T,U> for a specific number, and if it doesn't exist I get the closest lower-bound key-item instead. I saw that it was rather slow in inserting unsorted data which I'm doing a lot of. Is there a way to do something similar with the SortedDictionary , or should I just stick to my SortedList ? 回答1: SortedList<K, V> is really slow when inserting data as it shifts <=N elements in internal array each time a new element is added. The complexity

C# — Need an IDictionary implementation that will allow a null key

孤人 提交于 2020-01-03 07:02:44
问题 Basically, I want something like this: Dictionary<object, string> dict = new Dictionary<object, string>(); dict.Add(null, "Nothing"); dict.Add(1, "One"); Are there any built into the base class library that allow this? The preceding code will throw an exception at runtime when adding the null key. Thanks 回答1: You could avoid using null and create a special singleton value class that does the same thing. For example: public sealed class Nothing { public static readonly Nothing Value = new

C# — Need an IDictionary implementation that will allow a null key

我们两清 提交于 2020-01-03 07:02:08
问题 Basically, I want something like this: Dictionary<object, string> dict = new Dictionary<object, string>(); dict.Add(null, "Nothing"); dict.Add(1, "One"); Are there any built into the base class library that allow this? The preceding code will throw an exception at runtime when adding the null key. Thanks 回答1: You could avoid using null and create a special singleton value class that does the same thing. For example: public sealed class Nothing { public static readonly Nothing Value = new

How HashMap works internally

家住魔仙堡 提交于 2020-01-03 06:42:08
问题 HashMap objHashMap = new HashMap(); objHashMap.put("key1", "Value1"); objHashMap.put("key1", "Value2"); System.out.println(objHashMap.get("key1")); Above code displaying "Value2" how and why 回答1: check this /** * Associates the specified value with the specified key in this map. If the * map previously contained a mapping for the key, the old value is * replaced. * * @param key * key with which the specified value is to be associated * @param value * value to be associated with the specified

How to consolidate a Dictionary with multiple (three) keys by reducing one or more key and merging the multiple records (if any) into one single one?

只谈情不闲聊 提交于 2020-01-03 05:44:09
问题 I have a Dictionary with the following definition. Dictionary<int[], int> D = new Dictionary<int[], int>(); where the key is a 3 element array. I am giving this as example to simplify my scenario. (In my own code the Key is a complex class object that has a List of 3-7 elements in it for key.) int[] key; key = new int[] { 1, 1, 1 }; D.Add(key, 1); key = new int[] { 1, 1, 2 }; D.Add(key, 2); key = new int[] { 1, 1, 3 }; D.Add(key, 3); key = new int[] { 1, 2, 4 }; D.Add(key, 4); key = new int[]

Fastest way to convert IEnumerable<T> to List<T> in C#

旧街凉风 提交于 2020-01-03 04:51:14
问题 In C#, what is the fastest way to create and fill a List using an IEnumberable in terms of time required to write the code for? What about in terms of time required to execute? My first thought was this: List<int> list = new List<int>(); foreach(int number in iterator) list.Add(number); Is there a faster way? 回答1: When it comes to List<T> essentially you have 2 approaches, which I am trying to discuss below. For the sake of clarity lets assume, allocation of the List<T> takes constant time (C

What is a time complexity for sets and maps created with a factory methods Set.of() and Map.of()?

微笑、不失礼 提交于 2020-01-03 00:54:07
问题 In Java, when I create a set with Set.of() or a map with Map.of() what is the time complexity of the contains and get operations? Is it O(1)? 回答1: The Set.of and Map.of APIs return instances of JDK-private implementations. The performance of these implementations is not guaranteed by the specification. However, the APIs do return specific implementations about which performance statements can be made. Thus, the question is reasonable and is distinct from a (hypothetical) question such as

how to make all possible power set(or subset) from arrayList objects?

試著忘記壹切 提交于 2020-01-02 22:00:53
问题 Say I have the following class: class A { String name; Double value; } and a list of the above class objects which might have: [{f 2.1}, {c 1.1}, {a 0.3}... and so on] [{n 0.5}, {f 1.9}, {x 0.1}, {a 1.9}, {b 1.1}... and so on] ... and so on all I want is to do the followings: 1. Building power subsets from the internal list items(N.B: skip the single subsets). 2. Push the subset in another List as an object of the above class A like this: a. if f,c is a subset of 1st element then f,c would be