collections

Use-cases for Streams in Scala

In Scala there is a Stream class that is very much like an iterator. The topic Difference between Iterator and Stream in Scala? offers some insights into the similarities and differences between the two. Seeing how to use a stream is pretty simple but I don't have very many common use-cases where I would use a stream instead of other a...

Is it possible to create a collection of generic contrained types in .net?

Is something like this possible? Dim l As New List(Of T As Type Where GetType(BaseClass).IsAssignableFrom(T)) Note, my collection will be a collection of Types, not objects of type T - which I know is possible. ETA: The answers I've had so far are as expected - I didn't think it was possible. What I'm trying to get my head round is...

Redmine Plugins - Detecting enabling and disabling of modules

I'm working with an external framework (redmine) which has one Project model that has_many EnabledModules. Projects can have EnabledModules "attached" or "removed" via the module names, like this: class Project < ActiveRecord::Base ... has_many :enabled_modules, :dependent => :delete_all ... def enabled_module_names=(module_nam...

Iterating through a list in reverse order in java

I'm migrating a piece of code to make use of generics. One argument for doing so is that the for loop is much cleaner than keeping track of indexes, or using an explicit iterator. In about half the cases, the list (an ArrayList) is being iterated in reverse order by using an index. Can someone suggest a cleaner way of doing this (since...

c++ container for checking whether ordered data is in a collection

Hello, I have data that is a set of ordered ints [0] = 12345 [1] = 12346 [2] = 12454 etc. I need to check whether a value is in the collection in C++, what container will have the lowest complexity upon retrieval? In this case, the data does not grow after initiailization. In C# I would use a dictionary, in c++, I could either use a h...

Returning an immutable collection when the items must be mutable initially

One example of the general case: public class Validator { // ... public ReadOnlyCollection<InvalidContainer> ContainersUnderMinimum { get { return _containersUnderMinimum.AsReadOnly(); } } } public class InvalidContainer { // ... public int LowestVolume { get; set; } } The Validator class above takes a collect...

Tree Collection in .NET

When I make a list box in WPF I frequently set its ItemsSource to be a List. Is there a Tree for TreeView (or what goes in ItemsSource for TreeView)? Is there a collection or generally accepted method for handling tree data in C#.NET? ...

Editing ObservableCollection's SelectedItem Without Bound Controls Seeing Edits Until They Are Saved

I have a view containing a ListView and an "Edit" Button. The ListView's ItemSource is bound to an ObservableCollection<Account> property on the underlying view model. Its SelectedItem property is also bound to the view model. When the edit button is clicked, the existing view model launches an editing view/view model pair ("editing s...

OOC: What is the difference between ToList() and casting to List<T> in .NET?

OOC: Out Of Curiosity So, as a little exercise and for the sake of learning, I decided to check if I was able to implement a very basic recursive function that would return a List<int>, but with the following restrictions: 1- The result should be returned by the function itself (as opposed to passed as an argument to a void function). ...

Why is there no IArray(T) interface in .NET?

Question (in short): I asked why .NET has IList<T>, which implements ICollection<T> and therefore provides methods to modify the list (Add, Remove, etc.), but doesn't offer any in-between interface such as IArray<T> to provide random access by index without any list modification. EDIT 2010-Jan-21 2:22 PM EST: In a comment to Jon Skeet...

Duplicate values in the Set collection?

Is it possible to allow duplicate values in the Set collection? Is there any way to make the elements unique and have some copies of them? Is there any functions for Set collection for having duplicate values in it? ...

Using key and values in collections

Hi, Is there any way to make a key for searching the values in the collections and not returning null keys and values? ...

Which is more efficient, a for-each loop, or an iterator?

Which is the most efficient way to traverse a collection? List<Integer> a = new ArrayList<Integer>(); for (Integer integer : a) { integer.toString(); } or List<Integer> a = new ArrayList<Integer>(); for (Iterator iterator = a.iterator(); iterator.hasNext();) { Integer integer = (Integer) iterator.next(); integer.toString();...

Best practice to create typed collections/lists with internal or private Add, Remove functionality?

I frequently expose typed collections and lists. Often when exposing collections and lists I am not happy with the Add and Remove methods being public. In older versions of .Net I have implemented IEnumerable but this is a lot of work. What are the better alternatives? I have seen questions similar to this, but not specifically about t...

push_back() and push_front() in Java

Is there any collection class in java, that implements push_back() and push_front() methods? ...

Is there an accepted Java equivalent to Python's zip()?

I've got two List objects and I want to pair them up, just like the zip() function in Python. I'm pretty sure this isn't available in the JDK, but is there something like this in a fairly widespread library, similar to Apache Commons Collections? Thanks. ...

Multimap with HashMultiset for values

Hi, I'm trying to have a (hash-based) Multimap with a (hash-based) Multiset of values for each key. See the example: Multimap<Object, Object> mmap = Multimaps.newMultimap( Maps.<Object, Collection<Object>>newHashMap(), new Supplier<Collection<Object>>() { public Collection<Object> get() { return HashMultiset.create(); ...

Distinct with custom comparer gives vauge error

I have the following code: IEqualityComparer<WorkItem> comparer = new LambdaComparer<WorkItem>((item1, item2) => item1.Type == item2.Type); var someVar = Pad.Distinct(comparer); (The idea is to get 1 of each type) And it is giving the following error message: The type arguments for method 'System.Linq.Enumerable.Distinct (Sy...

What is returned when Distinct is called with a custom comparer?

If have a class of Widgets like this: public class Widget { public double Price { get; set; } public string Type { get; set; } public int ID { get; set; } public string Name { get; set; } } And create a list of them: List<Widget> Widgets = new List<Widget> { new Widget {ID = 1, Name = "One", Price = 3.00, Type =...

Match elements between 2 collections with Linq in c#

Hi, i have a question about how to do a common programming task in linq. lets say we have do different collections or arrays. What i would like to do is match elements between arrays and if there is a match then do something with that element. eg: string[] collection1 = new string[] { "1", "7", "4" }; string[] collecti...