collections

Return collection as read-only

I have an object in a multi-threaded environment that maintains a collection of information, e.g.: public IList<string> Data { get { return data; } } I currently have return data; wrapped by a ReaderWriterLockSlim to protect the collection from sharing violations. However, to be doubly sure, I'd like to return the col...

Are non-generic collections in .NET obsolete?

Put differently: Is there a good reason to choose a loosely-typed collection over a type-safe one (HashTable vs. Dictionary)? Are they still there only for compatibility? As far as I understand, generic collections not only are type-safe, but their performance is better. Here's a comprehensive article on the topic: An Extensive Exam...

Which .Net collection for adding multiple objects at once and getting notified ?

Was considering the System.Collections.ObjectModel ObservableCollection<T> class. This one is strange because it has an Add Method which takes one item only. No AddRange or equivalent. the Notification event arguments has a NewItems property, which is a IList (of objects.. not T) My need here is to add a batch of objects to a coll...

Using Small (1-10 Items) Instance-Level Collections in Java

While creating classes in Java I often find myself creating instance-level collections that I know ahead of time will be very small - less than 10 items in the collection. But I don't know the number of items ahead of time so I typically opt for a dynamic collection (ArrayList, Vector, etc). class Foo { ArrayList<Bar> bars = new Array...

Implements several interfaces with conflict in signatures

Lasty, I tried to implements an hybrid structure in Java, something that looks like: public class MapOfSet<K, V extends HasKey<K>> implements Set<V>, Map<K, Set<V>> Where HasKey is the following interface: public interface HasKey<K> { public K getKey(); } Unfortunately, there are some conflicts between methos signature of the S...

Detecting concurrent modifications?

In a multi-threaded application I'm working on, we occassionally see ConcurrentModificationExceptions on our Lists (which are mostly ArrayList, sometimes Vectors). But there are other times when I think concurrent modifications are happening because iterating through the collection appears to be missing items, but no exceptions are thro...

How to instantiate a Java array given an array type at runtime?

In the Java collections framework, the Collection interface declares the following method: T[] toArray(T[] a) "Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a ne...

Mapping a collection of enums with NHibernate

Mapping a collection of enums with NHibernate Specifically, using Attributes for the mappings. Currently I have this working mapping the collection as type Int32 and NH seems to take care of it, but it's not exactly ideal. The error I receive is "Unable to determine type" when trying to map the collection as of the type of the enum I ...

Most efficient way to increment a Map value in Java

I hope this question is not considered too basic for this forum, but we'll see. I'm wondering how to refactor some code for better performance that is getting run a bunch of times. Say I'm creating a word frequency list, using a Map (probably a HashMap), where each key is a String with the word that's being counted and the value is an I...

In C# .NET 2.0, what's an easy way to do a foreach in reverse?

Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>(); Now I want to iterate through the dictionary in reverse order. I can't use a simple for loop because I don't know the keys of the dictionary. A foreach is easy: foreach (SomeObject object in myDictionary.Values) { ...

Collection initialization syntax in VB 2008?

I'm trying to determine if there's a way in Visual Basic 2008 (Express edition if that matters) to do inline collection initialization, a la JavaScript or Python: Dim oMapping As Dictionary(Of Integer, String) = {{1,"First"}, {2, "Second"}} I know VB2008 supports array initialization like this, but I can't seem to get it to work for co...

Does the unmodifiable wrapper for java collections make them thread safe?

I need to make an ArrayList of ArrayLists thread safe. I also cannot have the client making changes to the collection. Will the unmodifiable wrapper make it thread safe or do I need two wrappers on the collection? ...

How do I iterate a .Net IList collection in the reverse order?

I have an IList that contains items ( parent first ), they need to be added to a Diagram Document in the reverse order so that the parent is added last, drawn on top so that it is the first thing to be selected by the user. What's the best way to do it? Something better/more elegant than what I am doing currently which I post below.. ...

Performance when checking for duplicates

I've been working on a project where I need to iterate through a collection of data and remove entries where the "primary key" is duplicated. I have tried using a List<int> and Dictionary<int, bool> With the dictionary I found slightly better performance, even though I never need the Boolean tagged with each entry. My expectation ...

How do you query object collections in Java (Criteria/SQL-like)?

Suppose you have a collection of a few hundred in-memory objects and you need to query this List to return objects matching some SQL or Criteria like query. For example, you might have a List of Car objects and you want to return all cars made during the 1960s, with a license plate that starts with AZ, ordered by the name of the car mode...

Fastest way to find objects from a collection matched by condition on string member

Suppose I have a collection (be it an array, generic List, or whatever is the fastest solution to this problem) of a certain class, let's call it ClassFoo: class ClassFoo { public string word; public float score; //... etc ... } Assume there's going to be like 50.000 items in the collection, all in memory. Now I want to obtain as f...

What is Your Favourite Area of the Java API?

I'm curious to know what other Java programmers feel is their favorite part of the language, why they feel that way, and why other programmers should want an intimate knowledge of it as well. I'm looking for reasons like simplicity, performance, etc. Thanks. ...

Accessing a Collection Through Reflection

Is there a way to iterate (through foreach preferably) over a collection using reflection? I'm iterating over the properties in an object using reflection, and when the program gets to a type that is a collection, I'd like it to iterate over the contents of the collection and be able to access the objects in the collection. At the mome...

Why aren't Java Collections remove methods generic?

Why isn't Collection.remove(Object o) generic? Seems like Collection<E> could have boolean remove(E o); Then, when you accidentally try to remove (for example) Set<String> instead of each individual String from a Collection<String>, it would be a compile time error instead of a debugging problem later. ...

Is it a good (correct) way to encapsulate a collection?

class MyContainedClass { }; class MyClass { public: MyContainedClass * getElement() { // ... std::list<MyContainedClass>::iterator it = ... // retrieve somehow return &(*it); } // other methods private: std::list<MyContainedClass> m_contained; }; Though msdn says std::list should not perform relocations of elements...