collections

Interview Question: .Any() vs if (.Length > 0) for testing if a collection has elements

In a recent interview I was asked what the difference between .Any() and .Length > 0 was and why I would use either when testing to see if a collection had elements. This threw me a little as it seems a little obvious but feel I may be missing something. I suggested that you use .Length when you simply need to know that a collection ha...

What's the fastest Java collection for single threaded Contains(Point(x,y)) functionality?

In my application I need to check a collection of 2D coordinates (x,y) to see if a given coordinate is in the collection, it needs to be as fast as possible and it will only be accessed from one thread. ( It's for collision checking ) Can someone give me a push in the right direction? ...

How to delete entries from a dictionary using the value

I have a dictionary collection as bleow: mydic.addvalue(key1, val1) mydic.addvalue(key2, val1) mydic.addvalue(key3, val1) mydic.addvalue(key4, val2) mydic.addvalue(key5, val2) From the above dictionary I want to delete all the entries where value == "val1", so that the result would have only following entry: mydic.addvalue(key4, val2...

WPF DataBinding with Constructor

(For this example) ListBox l is bound to CustomObjectCollection c. Does l call c's Constructor? What if c is a Generic Object? **In XAML (1)** <ListBox Content={Binding CustomObjectCollection}/> **In Codebehind** CustomObjectCollection<MyClass> c; **In XAML (2)** <ListBox Content={Binding CustomObjectCollection}/> Suppose in c, I po...

Why do we have contains(Object o) instead of contains(E e)?

Is it to maintain backwards compatibility with older (un-genericized) versions of Collection? Or is there a more subtle detail that I am missing? I see this pattern repeated in remove also (remove(Object o)), but add is genericized as add(E e). ...

Is there any way to get the classes from System.Collections.Concurrent in .NET 3.5?

The question pretty much says it all. Does anyone know of any way to get the collection classes from the System.Collections.Concurrent namespace in .NET 3.5? I guess what I'm asking is whether anyone has gone through the trouble of porting these classes to their own DLL that could be accessed from .NET 3.5. ...

Is there something like List<String, Int32, Int32> (multidimensional generic list)

I need something similar to List<String, Int32, Int32>. List only supports one type at a time, and Dictionary only two at a time. Is there a clean way to do something like the above (a multidimensional generic list/collection)? ...

How to pop items from a collection in Java?

Is there a method in JDK or apache commons to "pop" a list of elements from a java.util.List? I mean, remove the list of elements and return it, like this method: public Collection pop(Collection elementsToPop, Collection elements) { Collection popped = new ArrayList(); for (Object object : elementsToPop) { if (elements.contai...

NameValueCollection vs Dictionary<string,string>

Possible Duplicate: IDictionary<string, string> or NameValueCollection Any reason I should use Dictionary<string,string> instead of NameValueCollection? (in C# / .NET Framework) Option 1, using NameValueCollection: //enter values: NameValueCollection nvc = new NameValueCollection() { {"key1", "value1"}, {"key2", "value2...

Collections removeAll method

I would like to know if something like below is possible , list<MyObject>.removeAll(list<String>) I hope the context is understandable. The list<MyObject> : is a ArrayList<MyObject> The list<String> : is a ArrayList<String> I know this can be achieved by overriding equals method in MyObject class. I would like to know if any ot...

Disable Adding Item to Collection

Hi All, I'm sure there's an "easy" answer to this, but for the moment it escapes me. In an MVVM application, I have a property that is a ObservableCollection, used for displaying some set of elements on the view. private readonly ObservableCollection<MyType> mMyCollection = new ObservableCollection<MyType>(); public ObservableCol...

Using Castor, how do you map Java class "java.util.ArrayList" to element "ArrayList", but also generate elements for the objects that it contains?

Using Castor, how do you map Java class java.util.ArrayList to element <ArrayList/> instead of <array-list/> while still including the elements that it contains? For example, the class mapping <class name="java.util.ArrayList"> <map-to xml="ArrayList" /> </class> maps an ArrayList object to an empty element, omitting elements fo...

Minimal framework in Scala for collections with inheriting return type

Suppose one wants to build a novel generic class, Novel[A]. This class will contain lots of useful methods--perhaps it is a type of collection--and therefore you want to subclass it. But you want the methods to return the type of the subclass, not the original type. In Scala 2.8, what is the minimal amount of work one has to do so tha...

Clojure: seq (cons) vs. list (conj)

I know that cons returns a seq and conj returns a collection. I also know that conj "adds" the item to the optimal end of the collection, and cons always "adds" the item to the front. This example illustrates both of these points: user=> (conj [1 2 3] 4) //returns a collection [1 2 3 4] user=> (cons 4 [1 2 3]) //returns a seq (4 1 2 3...

Threading and iterating through changing collections

In C# (console app) I want to hold a collection of objects. All objects are of same type. I want to iterate through the collection calling a method on each object. And then repeat the process continuously. However during iteration objects can be added or removed from the list. (The objects themselves will not be destroyed .. just remo...

Entity Framework: a proxy collection for displaying a subset of data

Imagine I have an entity called Product and a repository for it: public class Product { public int Id { get; set; } public bool IsHidden { get; set; } } public class ProductRepository { public ObservableCollection<Product> AllProducts { get; set; } public ObservableCollection<Product> HiddenProducts { get; set; } } Al...

C# Winforms: PropertyGrid not updated when item added to Collection

I've got a custom class which can be edited through the PropertyGrid. In that class I've got a custom Collection (with custom PropertyDescriptor and TypeConverter). Items can be added to or removed from the Collection with the default Collection Editor. This all works fine. But - after closing the Collection Editor, the PropertyGrid is...

Memory allocation for collections in .NET

This might be a dupe. I did not find enough information on this. I was discussing memory allocation for collections in .Net. Where is the memory for elements allocated in a collection? List<int> myList = new List<int>(); The variable myList is allocated on stack and it references the List object created on heap. The question is wh...

Storing each thread's data when using Semaphore

My task was to deal with problem of 5 eating thinkers. Each of them can eat only 10 times. Solution should base on Semaphore. I almost solved the task and output was correct when I simply used println for showing state of each thinker. But there is additional point to store those states in some sort of collection. And somehow I can't mak...

Java Double entry table

Hi, Does anyone know a double entry table implementation in java I can download ? I need to do something like this 1 2 3 _______ a| x y z b| h l m c| o a k table.get(a,1) would return x Of course, it should use any Object as key, value, etc Thanks in advance ...