collections

C# Extension method for AddItem to IEnumerable<T>

What is the best way to add an item to IEnumerable collection using Extension method? ...

Designing a convenient default valued map in Scala

I find myself using a lot of nested maps, e.g a Map[Int, Map[String, Set[String]]], and I'd like to have new Maps, Sets, etc. created automatically when I access a new key. E.g. something like the following: val m = ... m(1992)("foo") += "bar" Note that I don't want to use getOrElseUpdate here if I don't have to because it gets pretty...

Most optimized/effective way to convert a collection into an array?

I know it must have been asked more than twice, but I could not find any applicable question to answer the matter. I have a collection which I wish to retrieve an array from in .NET 2.0. In other words, I want to convert a collection into an array. So far I have the following: Public Class Set(Of T) Implements IEnumerable(Of T) ...

Most efficient way of finding an item at a certain index in a List<>, modifying the item and then updating the List<>?

Take List<Car>. Each Car has a unique index identifying it, say RegNumber and then another property describing it - say Color for example. I want to check if the collection has a car with RegNumber 5 if it does, change the color if it doesn't, add a new item for that car save the list This is the way I am currently doing it and I'm...

upcast from List<subclass> to List<superclass> via List<?>

I have a class A and a class B extends A In another class C I have a field private List<B> listB; Now, for some unusual reason, I have to implement this method in C public List<A> getList(); I tried to do so by forcing an upcast of listB field to List<A> via a List<?> cast: public List<A> getList(){ return (List<A>)(List<?>)l...

DataGrid - Displaying Running Total From Top of Grid Through Current Row

Hello, How would I display a running total for a particular column of each record that is displayed in a DataGrid? I'd like to keep the computation logic in the view model. Desired Output: Value | Running Total 25 25 10 35 -2 33 Thank you, Ben Edit: Keep in mind that when a user-triggered DataGri...

ItemsControl - Bind Control's and Backing Collection's Sort Orders Together

Hello, Is there a way to bind an ItemsControl (like ListView or DataGrid) to a sorted collection such that: whenever the collection is resorted via code, the ItemsControl automatically reflects the new sort order, AND whenever the ItemsControl is sorted, it re-sorts the collection? Thank you, Ben ...

Two way collections in JAVA

I have a list of objects. The objects are given ID and stored in the Hashtable. If I need a object with particular ID, I simply say ht.get(ID); However, sometimes, I need to get ID for given object ht.get(Object); The first idea is to use two different HashTables one for ID -> Object mapping and the order for Object -> ID mapping. D...

HashMap.containsKey(Object key) can't find a key

Sure I do something wrong, but can't realize what :) I populate a key/value pair collection Map keyValuePairs with data, after I try to check collection for specific key existence in a common way keyValuePairs.containsKey("Name") and get back "false". But keyValuePairs.keySet() gives back set of keys where "Name" exists. public static v...

Exists in nested collection cannot find without ID?

Hey guys another rails issue, Currently have a collection that is line items for an invoicing system. I want to increment the count of the line items if I add in an item that already exists. At the moment I'm using an exists? query on the collection but it seems to return regardless of the key. The foreign key I'm using is item_id, so ...

Is it faster the List<T>.Remove(T) or List<T>.RemoveAt(int) method?

Is it faster the List.Remove(T) or List.RemoveAt(int) method in .NET collections? Is speed different for value types or reference types? Thanks, Alberto ...

remove duplicated lines in ArrayList<ArrayList<String>>

public void removeDuplicates (ArrayList<ArrayList<String>> strings) { Set<ArrayList<String>> s = new LinkedHashSet<ArrayList<String>>(strings); strings = new ArrayList<ArrayList<String>>(s); } i want to remove duplicated lines in ArrayList<ArrayList<String>>, I want to use LinkedHashSet and compare ArrayList between themselve...

Access Derived class properties from base class collection

Hi, I have created a derived class from a base class and have added the derived class objects to the base class collection.Later when i try to cast the collection objects to derived class,it is throwing error and i am not able to get my override methods and properties.How could i get around this.? I am trying to override the stroke clas...

C# multi-threaded reading of unmodifiable collections

I have a dictionary which is filled once in a static constructor and stays unchanged ever since. I want multiple threads to be able to read values from this collection. Do I need any locking here? ...

Java: One constructor or method that will accept array or set or list or ...?

In Java is there anyway to have one constructor that will accept an array or a collection? I have been fiddling with this for a while but I don't think it is possible. I would like to be able to initialize MyClass, like this: MyClass c = new MyClass({"rat", "Dog", "Cat"}); And like this: LinkedList <String> l = new <String> Link...

How to Create a Method that only accepts elements that implement Iterable

I want to write a function printAll(), which accepts only those elements that implement that implement Iterable so that I can iterate over them and print the elements. How do I do that? ...

Android: retrieving all Drawable resources from Resources object

In my Android project, I want to loop through the entire collection of Drawable resources. Normally, you can only retrieve a specific resource via its ID using something like: InputStream is = Resources.getSystem().openRawResource(resourceId) However, I want to get all Drawable resources where I won't know their ID's beforehand. Is ...

Can't populate VBA Collection with Custom Objects using Collection.add

I don't understand why the Debug.Print n & " - " & objTrans2.DESC line at the bottom of this code is outputting "Description 2" twice. I want it to output "Description 1" and "Description 2". Is there something wrong with how I am trying to add a custom object (Transaction) through the sampleCollection.Add from within the For loop? Th...

mapping collection type with Fluent NHibernate

Hello guys. I've used Fluent NH in my project but I'm having some problems with using the Collection class. Here's the code for my classes public class Vendor { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual Services Services { get; set; } } public class Services : I...

Informing PersistenceManager about change

Hi, I am building an app with Java witch will be deployed on Google App Engine. I on of my persistence capable object I have an Collection of Strings. Is there a way to inform PersistenceManager about an change in this collection, while, as I understand, PersistenceManager only detects changes when property have changed. Luke. ...