collections

Accessing a property of derived class from the base class in C#

In C#, was is the best way to access a property of the derived class when the generic list contains just the base class. public class ClassA : BaseClass { public object PropertyA { get; set; } } public class ClassB: BaseClass { public object PropertyB { get; set; } } public class BaseClass { } public void Main { List<BaseC...

Why not allow an external interface to override hashCode/equals for a HashMap?

With a TreeMap it's trivial to bypass the keys' natural ordering using a Comparable. HashMaps however cannot be controlled in this manner. I suspect it would be both easy and useful to design an interface and to retrofit this into HashMap (or a new class)? Something like this, except with better names: interface Hasharator<T> { ...

What is the best way to localize an IEnumerable?

What is the best way to localize a collection (IEnumerable)? From the BL I retrieve a collection of entities which still need to localized, I figured I write a method which extends the IEnumerable and returns the localized list. How can i get the code underneath working? Any ideas? Maybe better options? public static IEnumerable L...

What dotnet collection class's items can be enumerated in "addition order" and retrieved via a key?

I'm lead to believe that I cannot count on the order of items added to a dictionary for enumeration purposes. Is there a class (generic if possible) to which items may be added with a key and which can be enumerated in addition order or which can be retrieved by key? Clarification: I do not want to enumerate in Key Order. I want to enu...

Is there a type-safe Java implementation of 'reduce'?

I often need to run reduce (also called foldl / foldr, depending on your contexts) in java to aggregate elements of an Itterable. Reduce takes a collection/iterable/etc, a function of two parameters, and an optional start value (depending on the implementation details). The function is successively applied to an element of the collec...

Java: Efficient Equivalent to Removing while Iterating a Collection

Hello everyone. We all know you can't do this: for (Object i : l) if (condition(i)) l.remove(i); ConcurrentModificationException etc... this apparently works sometimes, but not always. Here's some specific code: public static void main(String[] args) { Collection<Integer> l = new ArrayList<Integer>(); for (int i=0; i < 1...

API java 5 and more: should I return an array or a Collection ?

In the spirit of Best Practices: Always return a ____, never a ____, I face a similar question in my upcoming migration from JDK1.4.2 to JDK5 and more. (Yes, I know, JDK1.4.2 is EOL! ;-) ). For functions returning a collection (which are not simple property collections), I always prefer (in JDK1.4.2) returning an Array instead of a gene...

Best practice for a collection of generic classes

Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int> { // blah } class SomeClassZ: SomeClassX<long> { // blah } I want a collection of SomeClassX<T>'s, however, this isn't possible since SomeClassX<int> != SomeClassX<long> and List<SomeClassX<>> isn't allowed. So my solution ...

Outputing a manipulated QueryString in c#

Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something like: &hello=world&microsoft=sucks But when I use this code to clone the collection to another object (of the same type) I get the Type() back from the ToString() method instead. System.Collections.Specialized.NameValueCollection ...

HashSet problem -- equals and hashCode with contains working differently than I'd expect

I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj): return self.tag.equals(obj.tag) def hashCode(self): return self.tag.hashCode() from java.lang import String from java.util import HashMap from java.util import HashSet tag1 = IncidentTag(Str...

what is the fastest way to generate a unique set in .net 2

I have what is essentially a jagged array of name value pairs - i need to generate a set of unique name values from this. the jagged array is approx 86,000 x 11 values. It does not matter to me what way I have to store a name value pair (a single string "name=value" or a specialised class for example KeyValuePair). Additional Info: There...

Getting i-th value from a SortedList or SortedDictionary

Hi, I have a sorted collection of objects (it can be either SortedList or SortedDictionary, I will use it mainly for reading so add performance is not that important). How can I get the i-th value? So e.g. when I have numbers 1, 2, 3, 4, 5 in the collection and I want the median (so 3 in this example), how can I do it? ...

Is a Collection of Collections possible and/or the best way? C# .Net 3.5

I have created a class for a dashboard item which will hold information such as placement on the dashboard, description, etc. I am currently using a pair of Collections to hold those dashboard items contained in the "library" and those items showing on the dashboard itself. I have been asked to make this dashboard multi-tab, and my fir...

How do I convert a List(Of T) to an ObservableCollection(Of T) in VB.NET?

Is there a way to do this without iterating through the List and adding the items to the ObservableCollection? ...

Threading issues in a Java HashMap

Something happened that I'm not sure should be possible. Obviously it is, because I've seen it, but I need to find the root cause & I was hoping you all could help. We have a system that looks up latitude & longitude for a zipcode. Rather than access it every time, we cache the results in a cheap in-memory HashTable cache, since the l...

Deleting items from one collection in another collection

I've got two collections (generic Lists), let's call them ListA and ListB. In ListA I've got a few items of type A. In ListB I've got some items of type B that have the SAME ID (but not same type) as the items in ListA, plus many more. I want to remove all the items from ListB that have the same ID as the ones in ListA. What's the best ...

Dictionary<string, MyObject> or List<MyObject> with C# 3.5?

I often use Dictionary in C#2.0 with the first key as string that was containing a unique identifier. I am learning C#3.0+ and it seems that I can now simply use a List and simply do LINQ on that object to get the specific object (with the .where()). So, if I understand well, the Dictionary class has lost its purpose? ...

Custom collections - still worth the extra work?

Excuse me if I'm off on my terminology, I only have around 2.4 years of programming experience, mostly in .NET. Currently I'm one of two .NET developers in a mainframe shop, the other developer set the standards and is a great coder with a lot more experience plus a CS degree(I am 100% self taught). We use custom collections for every...

ordering by collection count in nhibernate

Is there a way of ordering a list of objects by a count of a property which is a collection? For arguments sake let's say I have a question object with a question name property, a property that is a collection of answer objects and another property that is a collection of user objects. The users join the question table via foreign key ...

Map of collections

I wanted to make Map of Collections in Java, so I can make something like public void add(K key, V value) { if (containsKey(key)) { get(key).add(value); } else { Collection c = new Collection(); c.add(value); put(key, value); } } I've tried to make it with something like public class ...