collections

How to sort a Map<Key, Value> on the values in Java?

I am relatively new to Java, and often find that I need to sort a Map on the values. Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through array sort with a custom comparator that sorts on the value associated with the key. Is there an easier way? ...

Logic and its application to Collections.Generic and inheritance

Everything inherits from object. It's the basis of inheritance. Everything can be implicitly cast up the inheritance tree, ie. object me = new Person(); Therefore, following this through to its logical conclusion, a group of People would also be a group of objects: List<Person> people = new List<Person>(); people.Add(me); people.Add(...

What is a solid, elegant, reusable piece of code for determining if an IEnumerable is empty, in .NET?

I'm trying to find the most reusable, yet elegant, piece of code possible for determining if an IEnumerable. In the ideal, this should be a function I can call absolutely any time I need to tell if an IEnumerable is empty. While I have developed an answer for .NET 3.5 that has worked well for me so far, my current thought is that there...

Java: What is the best way to filter a Collection?

I want to filter a java.util.Collection based on a predicate. ...

Testing if an Object is a Dictionary in C#

Is there a way to test if an object is a dictionary? In a method I'm trying to get a value from a selected item in a list box. In some circumstances, the list box might be bound to a dictionary, but this isn't known at compile time. I would like to do something similar to this: if (listBox.ItemsSource is Dictionary<??>) { KeyValu...

Controlling access to an internal collection in c# - Pattern required

This is kind of hard to explain, I hope my English is sufficient: I have a class "A" which should maintain a list of objects of class "B" (like a private List). A consumer of class "A" should be able to add items to the list. After the items are added to the list, the consumer should not be able to modify them again, left alone that he ...

When using a HashMap are values and keys guaranteed to be in the same order when iterating?

When I iterate over the values or keys are they going to correlate? Will the second key map to the second value? ...

Any good collection module in perl?

Can someone suggest a good module in perl which can be used to store collection of objects? Or is ARRAY a good enough substitute for most of the needs? Update: I am looking for a collections class because I want to be able to do an operation like compute collection level property from each element. Since I need to perform many such op...

When creating a collection via WebDAV should the name of the collection end with a slash

A WebDAV library I'm using is issuing this request MKCOL /collection HTTP/1.1 To which apache is issuing a 301 because /collection exists HTTP/1.1 301 Location: /collection/ Rather than a HTTP/1.1 405 Method Not Allowed The spec is a bit vague on this (or it could be my reading of it), but when issuing an MKCOL, should the ...

Which list<Object> implementation will be the fastest for one pass write, read, then destroy?

What is the fastest list implementation (in java) in a scenario where the list will be created one element at a time then at a later point be read one element at a time? The reads will be done with an iterator and then the list will then be destroyed. I know that the Big O notation for get is O(1) and add is O(1) for an ArrayList, while ...

Best place to Sort a List of Tasks

Hi, Im building a web application which is a process management app. Several different employee types will be shown a list of Tasks to do and as each one completes a task then it is moved on to the next employee to work on. The Task hierarchy is Batch > Load > Assembly > Part > Task. There are currently 8 rules for determining which T...

best way to pick a random subset from a collection?

I have a set of objects in a Vector from which I'd like to select a random subset (e.g. 100 items coming back; pick 5 randomly). In my first (very hasty) pass I did an extremely simple and perhaps overly clever solution: Vector itemsVector = getItems(); Collections.shuffle(itemsVector); itemsVector.setSize(5); While this has the adva...

What is the best data structure in .NET for look-up by string key or numeric index?

I'm looking for the most ideal data structure (for performance and ease of use) from which values can be retrieved by string key or index. Dictionary doesn't work because you can't really retrieve by index. Any ideas? ...

What is the best way to clone/deep copy a .NET generic Dictionary<string, T>?

I've got a generic dictionary Dictionary that I would like to essentially make a Clone() of ..any suggestions. ...

Java printf functionality for collections or arrays

In python you can use a tuple in a formatted print statement and the tuple values are used at the indicated positions in the formatted string. For example: >>> a = (1,"Hello",7.2) >>> print "these are the values %d, %s, %f" % a these are the values 1, Hello, 7.200000 Is there some way to use any array or collection in a java printf s...

Copy collection items to another collection in .NET

In .NET (VB), how can I take all of the items in one collection, and add them to a second collection (without losing pre-existing items in the second collection)? I'm looking for something a little more efficient than this: For Each item As Host In hostCollection1 hostCollection2.Add(item) Next My collections are generic collectio...

How to convert generic dictionary to non-generic without enumerating?

I want to convert an instance of generic IDictionary to non generic IDictionary. Can I do it without creating new instance of IDictionary? Is any framework support for this task? I tried wrap generic IDictionary in class that implements nongenetic IDictionary however I discovered that I have to also somehow convert generic ICollection ...

HashSet vs. List performance

It's clear that a search performance of the generic HashSet<T> class is higher than of the generic List<T> class. Just compare the hash-based key with the linear approach in the List<T> class. However calculating a hash key may itself take some CPU cycles, so for a small amount of items the linear search can be a real alternative to the...

What's the best way of implementing a thread-safe Dictionary in .NET?

I was able to implement a thread-safe Dictionary in C# by deriving from IDictionary and defining a private SyncRoot object: public class SafeDictionary<TKey, TValue>: IDictionary<TKey, TValue> { private readonly object syncRoot = new object(); private Dictionary<TKey, TValue> d = new Dictionary<TKey, TValue>(); public objec...

WPF Bind to a Collection of objects, sorted by IDs stored in order in another Collection in C#

For those of you that like puzzles: I had this problem recently and am sure there must be a nicer solution. Consider : an ObservableCollection of Foo objects called foos. Foo contains a string ID field I have no control over foos foos will be changing Then: I have another collection called sortLikeThis sortListThis contains string...