collections

.NET equivalent of Java's List.subList()?

别说谁变了你拦得住时间么 提交于 2019-12-19 05:13:59
问题 Is there a .NET equivalent of Java's List.subList() that works on IList<T> ? 回答1: using LINQ list.Skip(fromRange).Take(toRange - fromRange) 回答2: For the generic List<T> , it is the GetRange(int, int) method. Edit: note that this is a shallow copy, not a 'view' on the original. I don't think C# offers that exact functionality. Edit2: as Kamarey points out, you can have a read-only view: List<int> integers = new List<int>() { 5, 6, 7, 8, 9, 10, 11, 12 }; IEnumerable<int> view = integers.Skip(2)

Hashcode for NULL key in HashMap

隐身守侯 提交于 2019-12-19 05:13:17
问题 I was just reading about the difference between HashMap and HashTable class in java. There I found a difference that former allow null key and later doesn't privileges for the same. As far as the working of HashMap is concern I know that, it calls hashcode method on key for finding the bucket in which that key value pair is to be placed. Here comes my question: How hashcode for a null value is computed or Is there any default value for hashcode of null key (if so please specify the value)?

Lodash sort collection based on external array

安稳与你 提交于 2019-12-19 05:07:08
问题 I have an array with keys like so: ['asdf12','39342aa','12399','129asg',...] and a collection which has these keys in each object like so: [{guid: '39342aa', name: 'John'},{guid: '129asg', name: 'Mary'}, ... ] Is there a fast way to sort the collection based on the order of keys in the first array? 回答1: var sortedCollection = _.sortBy(collection, function(item){ return firstArray.indexOf(item.guid) }); 回答2: Input: var data1 = ['129asg', '39342aa']; var data2 = [{ guid: '39342aa', name: 'John'

Does HashTable maintains the insertion order?

删除回忆录丶 提交于 2019-12-19 05:06:26
问题 The following code gives me the output in the same order of insertion. I read the javadoc and they did not even talk about the insertion order. Can someone help me to get the right information. import java.util.*; public class hash { public static void main(String[] args) { String str[] = { "japan", "usa", "japan", "russia", "usa", "japan", "japan", "australia"}; int len = 8; Hashtable ht = new Hashtable(); int i = 0; while (i < len) { String c = str[i]; System.out.println("c :" + c); Integer

Does calling Clear disposes the items also?

我怕爱的太早我们不能终老 提交于 2019-12-19 05:04:32
问题 Many times there is a clear method, that removes all the items from the collections, are these items disposed also. Like, toolStripMenuItem.DropDownItems.Clear(); is sufficient, or should I have to call like that: foreach (ToolStripItem item in toolStripMenuItem.DropDownItems) { toolStripMenuItem.DropDownItems.Remove(item); item.Dispose(); } Edit: Well ToolStripItem is an example not a question, for those who says Clear is enough I found another example, TabControl has also item collection

How to sum values from Java Hashmap [duplicate]

蹲街弑〆低调 提交于 2019-12-19 05:04:07
问题 This question already has answers here : How do I efficiently iterate over each entry in a Java Map? (41 answers) Closed 5 years ago . I need some help, I'm learning by myself how to deal with maps in Java ando today i was trying to get the sum of the values from a Hashmap but now Im stuck. This are the map values that I want to sum. HashMap<String, Float> map = new HashMap<String, Float>(); map.put("First Val", (float) 33.0); map.put("Second Val", (float) 24.0); Ass an additional question,

How to sum values from Java Hashmap [duplicate]

不羁的心 提交于 2019-12-19 05:03:59
问题 This question already has answers here : How do I efficiently iterate over each entry in a Java Map? (41 answers) Closed 5 years ago . I need some help, I'm learning by myself how to deal with maps in Java ando today i was trying to get the sum of the values from a Hashmap but now Im stuck. This are the map values that I want to sum. HashMap<String, Float> map = new HashMap<String, Float>(); map.put("First Val", (float) 33.0); map.put("Second Val", (float) 24.0); Ass an additional question,

Storing duplicate key value pairs in C#

徘徊边缘 提交于 2019-12-19 05:03:11
问题 I have a data like in (string , int) pair. How to store this data in collection object. Both values can be duplicate. Which collection object should i use?? EDIT: How can i access elements separately..?? 回答1: You can use List<KeyValuePair<string,int>> . This will store a list of KeyValuePair 's that can be duplicate. 回答2: You can use List<KeyValuePair<string, int>> if you want to add & remove items, or KeyValuePair<string, int>[] if the number of items is known 回答3: If you want to avoid

Storing duplicate key value pairs in C#

别说谁变了你拦得住时间么 提交于 2019-12-19 05:02:49
问题 I have a data like in (string , int) pair. How to store this data in collection object. Both values can be duplicate. Which collection object should i use?? EDIT: How can i access elements separately..?? 回答1: You can use List<KeyValuePair<string,int>> . This will store a list of KeyValuePair 's that can be duplicate. 回答2: You can use List<KeyValuePair<string, int>> if you want to add & remove items, or KeyValuePair<string, int>[] if the number of items is known 回答3: If you want to avoid

How to bind WPF TreeView to a List<Drink> programmatically?

十年热恋 提交于 2019-12-19 04:56:31
问题 So I am very new to WPF and trying to bind or assign a list of Drink values to a wpf treeview, but don't know how to do that, and find it really hard to find anything online that just shows stuff without using xaml. struct Drink { public string Name { get; private set; } public int Popularity { get; private set; } public Drink ( string name, int popularity ) : this ( ) { this.Name = name; this.Popularity = popularity; } } List<Drink> coldDrinks = new List<Drink> ( ){ new Drink ( "Water", 1 ),