collections

Classical set operations for java.util.Collection

Is there any built-in functionality for classical set operations on the java.util.Collection class? My specific implementation would be for ArrayList, but this sounds like something that should apply for all subclasses of Collection. I'm looking for something like: ArrayList<Integer> setA ... ArrayList<Integer> setB ... ArrayList<Intege...

Make your collections thread-safe?

When designing a collection class, is there any reason not to implement locking privately to make it thread safe? Or should I leave that responsibility up to the consumer of the collection? ...

Is the order of objects returned by FOREACH stable?

Is it safe to assume that two itterations over the same collection will return the objects in the same order? Obviously, it is assumed that the collection has not otherwise been changed. ...

Is it possible to have a List<string> as a property on an active record class

Is it possible to have a HasMany relationship of a basic type such as String, on an ActiveRecord class, without the need for creating another entity such as (TodoListItem) to hold the value. [ActiveRecord] public class TodoList { [PrimaryKey] public int Id { get { return _id; } set { _id = value; } } [HasMany(typeof(...

Class property as a collection

Greetings, I need to include a property in my class which is a collection of System.IO.FileInfo objects. I am not really sure how to do this and how I would add and removed objects from an instance of the the class (I would assume like any other collection). Please let me know if I need to add more information. Thank you Update:...

Why are Array.Sort() and Array.IndexOf() methods static?

Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort() and ArrayList.IndexOf() are designed as member methods. Thank you for any ideas. ...

Using Collections API to Shuffle

I am getting very frustrated because I cannot seem to figure out why Collections shuffling is not working properly. Lets say that I am trying to shuffle the 'randomizer' array. int[] randomizer = new int[] {200,300,212,111,6,2332}; Collections.shuffle(Arrays.asList(randomizer)); For some reason the elements stay sorted exa...

How to Serialize Hibernate Collections Properly?

I'm trying to serialize objects from a database that have been retrieved with Hibernate, and I'm only interested in the objects' actual data in its entirety (cycles included). Now I've been working with XStream, which seems powerful. The problem with XStream is that it looks all too blindly on the information. It recognizes Hibernate's ...

C# Set collection?

Does anyone know if there is a good equivelent to Java's Set collection in C#. It's one of the few things I miss from having moved to C#. I am aware that you can have a "pretend" set using a Dictionary or Hashtable and only bothering to populate the keys. But it's not very elegant. It's wierd every other way I turn in C# somone seems...

C# Fastest Convert from Collection to List<T>

What I'd like to avoid: ManagementClass m = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection managementObjects = m.GetInstances(); List<ManagementObject> managementList = new List<ManagementObject>(); foreach(ManagementObject m in managementObjects){ managementList.Add(m); } Isn't there a way to get that ...

VB.NET Strong-typed collection

I want to create a collection in VB.NET, but I only want it to accept objects of a certain type. For example, I want to create a class called "FooCollection" that acts like a collection in every way, but only accepts objects of type "Foo". I thought I could do this using generics, using the following syntax: Public Class FooCollect...

Storing more than 1 data item at a single index in a linked-list?

I am trying to store more than 1 data item at a single index in my linked-list. All of the examples in my textbook seem to illustrate adding only 1 piece of data per index. I'm assuming it is possible to add more? For example, using the Collections API to store an integer I would do the following: LinkedList <Integer>linky = new Link...

improved collection Iterator

Hi, Personally, I find the range of functionality provided by java.util.Iterator to be fairly pathetic. At a minimum, I'd like to have methods such as: peek() returns next element without moving the iterator forward previous() returns the previous element Though there are lots of other possibilities such as first() and last(). Does...

Printing out items in any Collection in reverse order?

Okay, so I have the following problem in my Data Structures and Problem Solving using Java book: Write a routine that uses the Collections API to print out the items in any Collection in reverse order. Do not use a ListIterator. I'm not putting it up here because I want somebody to do my homework, I just can't seem to understand exact...

Where can I learn about the various types of .NET lists?

Does anyone know a good resource to concisely explain the different types of lists available in C# and when their usage is appropriate? For example, List, Hashtable, Dictionaries etc. I'm never quite sure when I should be using what. ...

IS it OK to use an int for the key in a KeyedCollection

Often times I need a collection of non-sequential objects with numeric identifiers. I like using the KeyedCollection for this, but I think there's a serious drawback. If you use an int for the key, you can no longer access members of the collection by their index (collection[index] is now really collection[key]). Is this a serious enough...

What is your best resource about generics and their interfaces?

I have found many pieces of documentations and recommendations about IEnumerator, IEnumerable, ICollection, IList and their generic counterparts. Sadly, I didn't find yet a tutorial or book which explains the whole hierarchy of interfaces, generic implementations of those interfaces and the best usage of each type of those. What was you...

How to map sorted index back to original index for collection I'm sorting.

I've got a collection (List<Rectangle>) which I need to sort left-right. That part's easy. Then I want to iterate through the Rectangles in their original order, but easily find their index in the sorted collection. indexOf() won't work, since I may have a number of equal objects. I can't help feeling there should be an easy way to do th...

Best way to remove items from a collection

What is the best way to approach removing items from a collection in C#, once the item is known, but not it's index. This is one way to do it, but it seems inelegant at best. //Remove the existing role assignment for the user. int cnt = 0; int assToDelete = 0; ...

Best approach to use in Java 6 for a List being accessed concurrently

I have a List object being accessed by multiple threads. There is mostly one thread, and in some conditions two threads, that updates the list. There are one to five threads that can read from this list, depending on the number of user requests being processed. The list is not a queue of tasks to perform, it is a list of domain objects t...