collections

compact syntax for instantiating an initializing collection

I'm looking for a compact syntax for instantiating a collection and adding a few items to it. I currently use this syntax: Collection<String> collection = new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3" })); I seem to recall that there's a more compact way of doing this that uses an anonymous subclass of ArrayList, th...

Java Collections automatic reallocation when size is reached.

I'm not sure if I'm using the correct terms, but I am curious how it's determined how much to increase the size of a Collection in Java when it gets full? I've tried searching but I'm not really coming up with anything useful. So, if I have something like List l = new ArrayList(1); l.add("1"); l.add("2"); How does it determine how mu...

How to automatically update filter and/or sort order on CollectionViewSource, when an individual item's property changes?

Ok, so this question is related to Windows Phone 7/Silverlight (updated WP7 Tools, Sept 2010), specifically filtering an underlying ObservableCollection<T>. In mucking about with the WP7 template Pivot control application, I've run into an issue whereby changing an underlying item in an ObservableCollection<T>, does not result in the on...

With this .NET event, is it OK to pass in this IList instance?

Hi folks, i've got the following code :- while (....) { var foo = DoTheFooShakeShakeShake(..); foos.Add(foo); // foos is an IList<Foo>, btw and is new'd, above. if (foos.Count % 100 == 0) { var e = new CustomFooEventArgs { UserId = whatever, Foos = foos }; OnFooPewPew(this, e); foos.Clear(); ...

Collection of generic types

Hi folks, I have an object (form) which contains a collection (.Fields) which I want to contain instances of a generic class (FormField). The FormField, simply, is defined as such: public class FormField<T> { private Form Form; public T Value { get; set; } public string Name { get; set; } public void Process() { ...

"Cosmetic" clean-up of old, unknown code. Which steps, which order? How invasive?

When I receive code I have not seen before to refactor it into some sane state, I normally fix "cosmetic" things (like converting StringTokenizers to String#split(), replacing pre-1.2 collections by newer collections, making fields final, converting C-style arrays to Java-style arrays, ...) while reading the source code I have to get fa...

Is it Possible to Nest Collections within Collections using Wpf DataGrid?

I want a simple sample program that nests collections within collections using Wpf DataGrid. ...

cast LinkedHashMap to HashMap in groovy

How do I convert LinkedHashMap to java.util.HashMap in groovy? When I create something like this in groovy, it automatically creates a LinkedHashMap even when I declare it like HashMap h = .... or def HashMap h = ... I tried doing: HashMap h = ["key1":["val1", "val2"], "key2":["val3"]] and def HashMap h = ["key1":["val1", "val2"],...

In memory paging of a List<SomeType> help

I have a List<User> collection, and want to do in-memory paging using a start and end index. What's the best way to do this? ...

How to tell whether a collection exists in MongoDB using Mongoid?

Since Mongoid.master.collection() returns a collection even if the collection doesn't exist, we can use coll = Mongoid.master.collection('analyticsCachedResult') if coll.count == 0 # [...] end to test if it is an empty collection. Another method is to loop through Mongoid.master.collections.each do |c| return c if c.name == 'ana...

Avoiding n+1 selects with Cached Hibernate Associations or Caching Collections as a whole

I've got a one-to-many relationship: Parent record with n Child records. These records are frequently used and read-only and are good candidates for caching. Here is an approximation of my Hibernate mapping: `<class name="Parent" table="Parent> <cache usage="read-only"/> <id name="primary_key"/> <property name="natural_key"/> ...

Rails: Route for :collection that is both GET *and* POST?

I have a route that currently looks like this: map.resources :regions, :collection => {:select_for_payroll => :get}, has_one => :payroll How can I make the :select_for_payroll take both GET and POST? Many thanks! MrM ...

Handling object events inside collections

How do I properly handle the events of objects inside a collection? Example: I have a List<Ping> which asynchronously pings a number of servers. How can I tell if one of the items in the List have called the PingCompleted event? What if I add/remove Ping objects? ...

Clone a list of reference types that CANNOT implement ICloneable

Is there an easier/tidier way to deep clone a list of reference types that do not implement ICloneable. Currently have been looping through each object in the list like so: Dim myListCopy As New List(Of ListObj) For Each lo As ListObj In MyList myListCopy.Add(lo.ShallowCopy) Next The object ListObj contains only v...

How to loop a Magento collection?

Basically, I need to get a CSV file of my customers, generated automatically in a script every day. I've tried several ways, but they are too slow or actually exhausted of memory. *1) foreach through collection resource * $collection = Mage::getResourceModel('customer/customer_collection') ->addAttributeToSelect('email') ->addAttribute...

What to do if collections.defaultdict is not available?

Solaris python 2.4.3: from collections import defaultdict does not exist.. Please advise what could be an alternative to use multi-level dictionaries: dictOut['1']['exec'] = 'shell1.sh' dictOut['1']['onfailure'] = 'continue' ... dictOut['2']['exec'] = 'shell2.sh' dictOut['2']['onfailure'] = stop' many thanks applom ...

Thread-Safe Buffered Observable Priority Queue?

I'm writing a program where one thread needs to push items onto the queue, and one or more threads pops items off the queue and processes them. To avoid running out of memory, I'd like the producer thread to sleep when the queue gets full. Some items have a higher priority than others, so I'd like those to be processed first. If the item...

Under what circumstance System.Collections.ArrayList.Add throws IndexOutOfRangeException?

We are experiencing weird bug at production environment we cannot debug nor inject logging code. I am trying to figure this up but following stack trace confuse me. System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Collections.ArrayList.Add(Object value) at ... According to the MSDN Add method...

Saving object to file using Serialization in Java

I have a large amount of data stored in a Collection. I would like to save this data to a file. Is it a good idea to use Serialization? Or should I use a custom format to save the data, or save it as XML for example? (The elements in the Collection are custom classes. Do I need to implement a method that serializes the objects?) ...

WPF: arranging collection items in a grid

Hi, I would like to arrange items of a collection in a grid with a specific number of columns and rows (say 4x6). Each item exposes the dependency properties (integer) X and Y and should be placed in the relevant cell of the grid. Note that the collection can change during runtime which should update the grid items. I couldn't find any ...