collections

Simplified Collection initialization

拜拜、爱过 提交于 2019-12-30 10:47:06
问题 While initializing WF4 activities we can do something like this: Sequence s = new Sequence() { Activities = { new If() ..., new WriteLine() ..., } } Note that Sequence.Activities is a Collection<Activity> but it can be initialized without the new Collection() . How can I emulate this behaviour on my Collection<T> properties? 回答1: Any collection that has an Add() method and implements IEnumerable can be initialized this way. For details, refer to Object and Collection Initializers for C#. (The

Why are Array.Sort() and Array.IndexOf() methods static?

坚强是说给别人听的谎言 提交于 2019-12-30 09:32:53
问题 Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort() and ArrayList.IndexOf() are designed as member methods. Thank you for any ideas. 回答1: In my view Array class is basically a class representation of the fixed size arrays that we declare using [] in program (you can draw the analogy like int has it's class (structure) representation as System.Int32). Also Array class does not contain the actually array data in any instance variables

Why are Array.Sort() and Array.IndexOf() methods static?

拜拜、爱过 提交于 2019-12-30 09:32:25
问题 Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort() and ArrayList.IndexOf() are designed as member methods. Thank you for any ideas. 回答1: In my view Array class is basically a class representation of the fixed size arrays that we declare using [] in program (you can draw the analogy like int has it's class (structure) representation as System.Int32). Also Array class does not contain the actually array data in any instance variables

Sorting Hashtable by Order in Which It Was Created

不想你离开。 提交于 2019-12-30 09:29:27
问题 This is similar to How to keep the order of elements in hashtable, except for .NET. Is there any Hashtable or Dictionary in .NET that allows you to access it's .Index property for the entry in the order in which it was added to the collection? 回答1: A NameValueCollection can retrieve elements by index (but you cannot ask for the index of a specific key or element). So, var coll = new NameValueCollection(); coll.Add("Z", "1"); coll.Add("A", "2"); Console.WriteLine("{0} = {1}", coll.GetKey(0),

ConcurrentModificationException in unmodifiable collection [duplicate]

穿精又带淫゛_ 提交于 2019-12-30 08:25:12
问题 This question already has answers here : Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . I have this code below, and I'm getting a ConcurrentModificationException by executing the following line: filterCardsToDevice(getCollection()); the code: private List<MyClass> filterCardsToDevice(Collection<MyClass> col) { final List<MyClass> newList = new ArrayList<MyClass>(); for (MyClass myObj : col) { long id = myObj.getId(); if (id < 0 || id >

ConcurrentModificationException in unmodifiable collection [duplicate]

一曲冷凌霜 提交于 2019-12-30 08:24:09
问题 This question already has answers here : Why is a ConcurrentModificationException thrown and how to debug it (7 answers) Closed 10 months ago . I have this code below, and I'm getting a ConcurrentModificationException by executing the following line: filterCardsToDevice(getCollection()); the code: private List<MyClass> filterCardsToDevice(Collection<MyClass> col) { final List<MyClass> newList = new ArrayList<MyClass>(); for (MyClass myObj : col) { long id = myObj.getId(); if (id < 0 || id >

ICollection<T>.Contains on custom types

旧城冷巷雨未停 提交于 2019-12-30 08:15:44
问题 If I have a (reference - does it matter?) type MyType which does not override the Equals method, what heuristics will be used when determining if an ICollection<MyType> contains a given instance of the type? What's the best way to use my own heuristics (e.g. check for the equality of the Id property value)? 回答1: Because your type doesn't override Equals, the default implementation of Equals will be used, i.e. reference equality. So Contains will be true if the collection contains that very

How to get the difference between two maps Java?

微笑、不失礼 提交于 2019-12-30 08:05:45
问题 I have two maps as below : Map<String, Record> sourceRecords; Map<String, Record> targetRecords; I want to get the keys differ from each of the maps.i.e. It shows mapping keys available in sourceRecords but not in targetRecords. It shows mapping keys available in targetRecords but not in sourceRecords. I did it as below : Set<String> sourceKeysList = new HashSet<String>(sourceRecords.keySet()); Set<String> targetKeysList = new HashSet<String>(targetRecords.keySet()); SetView<String>

Is there a fast concat method for linked list in Java?

£可爱£侵袭症+ 提交于 2019-12-30 07:59:07
问题 How can I concat two linked lists in O(1) with Java via jdk1.6, google or apache commons collection or whatever? E.g. in the jdk there is only the addAll method which is O(n). Another feature I miss is to concat two lists where each of them could be in inverse order. To illustrate this assume two lists a->b->c and e->f->g could merged into a->b->c->e->f->g a->b->c->g->f->e c->b->a->e->f->g c->b->a->g->f->e Do you know of such a list implemenation or do I have to implement my own linked list?

WPF Datagrid group and sort

元气小坏坏 提交于 2019-12-30 07:24:09
问题 I am implementing grouping in WPF datagrid. I want to sort the grouped items. For example datagrid is having four columns(empno,name,dept,address). I am doing grouping by dept column. when I click on the dept column header I want to sort the grouped items. Here I am using ListCollectionView to group the items in the code behind. public ListCollectionView collection; collection = new ListCollectionView(obj.empData); collection.GroupDescriptions.Add(new PropertyGroupDescription("Country"));