collections

Collection with two keys

In the solution I have worked out to this question => General type conversion without risking Exceptions (See the edit at the bottom of the question), I need to cache a method for converting between two types. So, given Type1 and Type2 I need to retrieve a method. In the answer to this question => What is the best C# collection with tw...

Java: Why are wrapper classes needed?

On the very high level, I know that we need to "wrap" the priminitve data types, such as int and char, by using their respective wrapper classes to use them within Java collections. I would like to understand how Java collections work at the low level by asking: "why do we need to wrap primitive data types as objects to be able to use th...

Java Float become 0.0 after adding to ArrayList<T>

I have the following object : class Repeat{ private long startIndex; private long endIndex; private int length; private float repetitions; private float period; private int errors; private float percentOverlap; public void setPercentOverlap(float percentOverlap) { this.percentOverlap = percentOve...

Is there an easy way to navigate a List<t> object?

I have a WPF form that shows a contact (Name, Address and State). The GUI is bound to a CurrentContact object and they are stored in a List<Contact>. I would like to add buttons to the bottom: +-----+ +-----+ +-----+ +-----+ | << | | < | | > | | >> | +-----+ +-----+ +-----+ +-----+ Meaning first, previous, next an...

Does c#/.net x.x have an implementation of a doubly linked list (that can be iterated over backwards)?

I've been searching for the standard implementation of a doubly linked list in c# (so that I have a linked list I can iterate over backwards) and cannot find one. I feel like something so simple must have an implementation that I'm just missing. If it does exist, for which version of c#/.net does it exist? Reverse iteration in general ...

C# Single Generic Collection Method for Multiple Derived Classes.

Scenario I have a single base class and 2 (possibly 3) other classes that derive from that base class. //Base Class class RateInstrument { public string Ric { get; set; } public string Tenor { get; set; } public double Rate { get; set; } public DateTime Date { get; set; } public double Price { get; set; } ...

What is the BCL equivalent of GetValueElseAdd in PowerCollections

In Wintellect's PowerCollections, there's a GetValueElseAdd which works like this: if ( collection.GetValueElseAdd( key, ref value)) { // just added, value unmodified } Or // factory method only gets called if the key is not present. if ( collection.GetValueElseAdd( key, out value, () => thingFactory.CreateNew())) { // just a...

.NET XML Serialization and Null Collections

I have a class with some collections in them, and I would like to serialize instances of this class to XML without having to initialize the collections to be empty, and without having to implement IXmlSerializable. I don't care if it creates empty elements, or doesn't create the elements at all. Just that it works without having to ini...

List with multiple indexes

Given a generic List I would need some kind of index (in the database sense) that would allow me fast retrieval. The keys for this index would not be unique, so I can't use a dictionary. Here's what I have in mind: Given a class Foo { P1, P2, P3 } that may have data like this { "aaa", 111, "yes" } { "aaa", 112, "no" } { "bbb", 111, "no"...

ICollection<T>.Contains on custom types

If I have a (reference - does it matter?) type MyType which does not override the Equals method, what heuristics will be used when determining if an ICollection<MyType> contains a given instance of the type? What's the best way to use my own heuristics (e.g. check for the equality of the Id property value)? ...

How to return an unmodifiable view of a Java Trove collection?

I'd like to put unmodifiable wrappers around some of the Trove collections: I've checked the Trove documentation and I cannot seem to find an easy way to do it (I may have overlooked something obvious). So as of now every time I need such an unmodifiable wrapper I'm extending the Trove collection (for example TIntLongHashMap) and delega...

passing ImmutableSet in place of Set?

I have a method that expects a Set parameter. I want to pass in an empty set, and I don't want any side effects on the Set. I can do this with collections by passing in: Collections.unmodifiableSet(Sets.newHashSet()) But I want to pass in: ImmutableSet.of() If I do the former a Set<Object> is created and I get "method not appli...

How do I add a count property to an item in a collection in Rails?

I have a data model involving Users and Awards, and joined by a user_awards table. class User < ActiveRecord::Base :has_many :user_awards :has_many :awards, :through => :user_awards # awards the user has won end class Award < ActiveRecord::Base :has_many :user_awards :has_many :users, :through => :user_awards end I'd like th...

list of Java APIs for graph/network data structures

What are some good Java APIs for working with graphs (edges/nodes) as data structures? Please add references to similar SO questions in comments to this wiki. Please edit the list in this wiki entry directly. Please add summary description of your added projects as answers (one project per answer). JUNG Java Universal Network/Grap...

NOt able to modify object of struct in loop

I have a List of structure.In the loop i am trying to modify the object's property,which is happening,but when i (Quick look in Visual studio)look into the list object ,the new value is not reflecting.Is it by virtue that the structure's object cannot be modified when in a collection? I am using generics list with the struct as the type ...

Collection with fast removal/iteration/insertion that recycles objects in Android/Java programs?

I'm programing a game for Android. As an example, the game might involve bullets, enemies, gems etc. which need to be: created and destroyed in the game world during gameplay e.g. a bullet is fire and then disappears when it hits a wall. accessed a lot in sequence e.g. all updated in sequence, then all drawn in sequence. Based on wh...

Passing an Interface collection

Suppose you have the following class: class Car : IPainting { ... } Then a function like this: void AddCars(IEnumerable<Car> collection) Then a code snippet like this: Car bmw = new Car(); Car mercedes = new Car(); IPainting a = (IPainting) bmw; IPainting b = (IPainting) mercedes; IPainting[] paintings = new IPainting[] {a, b};...

Most of the Iterators and Iterables methods are LAZY! What does this mean.

1 of the presentation says "These methods are LAZY!" Iterable transform(Iterable, Function)* Iterable filter(Iterable, Predicate)* T find(Iterable<T>, Predicate) Iterable concat(Iterable<Iterable>) Iterable cycle(Iterable) T getOnlyElement(Iterable<T>) Iterable<T> reverse(List<T>) Can someone help me understand what they mean by this,...

Storing a collection in SQL

[pre-able] In my www.twipler.com project I want to allow people to link Twitter accounts. This will be achieved by having them login, selecting "link account" and then logging in again. This will effectively give me UserId-1 and UserId-2. Now I want to allow the user to login with either UserId-1 or UserId-2 credentials and then retreive...

Automatically creating a collection in the Dictionary<Key, Collection<Value>>

Lot of times I have to create a Dictionary<KeyType, List<ValueType>> Before I can start using the dictionary I have to first verify that List has been created for that key. //Can i remove these two lines? if(!dict.ContainsKey(key)) dict[key]= new List<ValueType>; //now use the key dict[key].Add(value); I know its only "2 lines"...