collections

Is Linq "Where" more expensive than Linq "Select" ?

I'm investigating performance on an operation. I'm iterating a subset of items from a collection. I filter this collection using a Linq query. It basically looks like this: var filteredItems = items.Where(x => x.PropertyToFilterOn == filterValue); foreach (var filteredItem in filteredItems) { // do something to the filtered item } ...

Subtracting two lists in Python

In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b. Note this is different from sets! I'm not interested in ...

Suitable collection class for event listeners in Java

Related: http://stackoverflow.com/questions/1391918/does-java-have-a-linkedconcurrenthashmap-data-structure I am looking for a collection class to hold references to event listeners. Ideally I would like the collection to have the following properties (in order of priority): Maintains insertion order. The earlier listeners may can...

.NET - Efficient collection of database entities?

I have a Page class and a PageCollection class in a 3d party ORM framework. I can fill the PageCollection based on parameters (pageid, parentid, url etc..) (SQL query). But I need the data multiple times around the ASP.NET MVC website (Sitemap, Authentication), so I chose to load all pages 1 time and reference that (global) collection. ...

Putting placeholders in a key/value List based on a Set of keys in Java

I have a Set of keys and a List of key/value pairs. The values are of the form Long,BigInteger. // key/values pairs: Long,BigInteger List<Object[]> values; // id list that corresponds to the keys for the list above Set<Long> ids; If any member of the key Set does not exist as a key in the key/value list, I want to add it to the list w...

Mapping a a simple collection of elements with Fluent NHibernate

I'm trying to map a collection of enum values with Fluent NHibernate. IList<EnumType> lst; I can't find any documentation about it but I'm quite sure it should be possible. I had no problem at all with mapping a collection of Entities. Thanks, Leonardo ...

Is static initialized unmodifiableCollection.get guaranteed immutable?

Is static initialized unmodifiableCollection.get guaranteed immutable? For: static final Map FOO = Collections.unmodifiableMap(new HashMap()); Can multiple threads use method get and not run into problems? Even through items in FOO cannot be added/removed, what's stopping the get method from manipulating FOO's internal state for ca...

Storing subcollections (filtered versions of the main collection) inside the collection as cache?

Is it good practice to store sub-collections of item X inside a parent collection of item X for caching 'filter-queries' which are performed on the parent collection? (They won't be used together (as in color AND type).) Or is it also ok to just Loop over the collection and return the correct entities in a temporary collection? Class It...

Transforming List using CollectionUtils throws ArrayStoreException

Java code: Transformer TRANSFORM_TO_INTEGER = new Transformer() { public Object transform(Object input) { Integer i = new Integer((String) input); return i; } }; String begin = "1,2,3,4,5"; List strList = Arrays.asList(StringUtils.split(begin, ",")); CollectionUtils.transform(strList, TRANSFORM_TO_INTEGER); Th...

An Analog of List.h in .Net

I used to use List.h to work with lists in C++, but are there any similar libraries in .Net ? Becouse I can't use List.h for managed types. ...

IQueryable<T> first free spot

What's the most efficient way to get a first free spot from sorted query returning int collection ? eg.: {1,2,3,4,6} | result.: 5 At the moment I am using foreach and counter that compares current value from sorted quety.ToList() which takes about 600ms on 100 000 records. ...

Is there an equivalent for Java WeakHashMap class in C#?

Is there a C# class that provides map with weak keys or/and weak values? Or at least WeakHashMap like functionality. ...

usability of synchronized... methods in java.util.Collections

I'm looking at the static method Collections.synchronizedList(List<T> list) Javadoc says It is imperative that the user manually synchronize on the returned list when iterating over it... What's the purpose of creating a synchronized list if I still have to manually synchronize it? ...

XmlSerializer bug when serializing collection of collections without root element?

This is a bit of a long question, but I've made it as terse as possible, so please bear with me. It looks to me like a bug in the XmlSerializer class but before I file it with Microsoft I'd like to see if there's anything I've missed, which is entirely possible. I'm attempting to generate the following XML as a representative case, whic...

Why is there a method iterator() on java.util.Collection

Why is there the method iterator() defined on the interface java.util.Collection when it already extends java.util.Iterable which has this very method defined. I'm thinking some sort of backward compatability or an opportunity to write some JavaDoc on the method at the collection level. Any other ideas? ...

Java Collections and Garbage Collector

A little question regarding performance in a Java web app. Let's assume I have a List<Rubrique> listRubriques with ten Rubrique objects. A Rubrique contains one list of products (List<product> listProducts) and one list of clients (List<Client> listClients). What exactly happens in memory if I do this: listRubriques.clear(); listRub...

How to test for an empty generic.dictionary collection?

How do I test a generic dictionary object to see whether it is empty? I want to run some code as follows: while (reportGraphs.MoveNext()) { reportGraph = (ReportGraph)reportGraphs.Current.Value; report.ContainsGraphs = true; break; } The reportGraph object is of type System.Collections.Generic.Dictionary When running this...

is there a .NET C# collection that supports fetch by both unique keys and non-unique fields?

My need is to have items in kind of Collections.Generic.Dictionary where I can get a struct by it's id as a key. Then I have need to fetch many structs, say 1% or less of all items, by another field. Like a cursor by an non-unique index. With Dictionary I have to browse through all the values and check which has the correct value for tha...

How to map a collection of abstract classes in nhibernate

I have been reading Nhibernate in Action, but the section on mapping polymorphic collections is a little too short on how to do this. I have the following code [Class] [Discriminator(Column="MachineType",TypeType=typeof(string))] public abstract class Machine { [Property] public string Name{get;set;} } [Subclass(DiscriminatorValue...

Need flexible Java key/value collection class for JComboBox

I have a model class that stores keys and values: public class KeyValue { private Object key; private String value; KeyValue () { } KeyValue (Object key, String value) { this.key=key; this.value=value; } public Object getKey() { return this.key; } public void setKey(Object ...