collections

Should I have methods which return lists of Disposable instances?

I have a class, instances of which need to be disposed. I also have several classes that produce these instances, either singly or lists of them. Should I return IList<MyClass> from my methods or should I create a class that is MyClassCollection which is also disposable and return this instead? EDIT: My main reason for asking is tha...

Security question: how to secure Hibernate collections coming back from client to server ?

Hello, I've got a simple pojo named "Parent" which contains a collection of object "Child". In hibernate/jpa, it's simply a one-to-many association, children do not know their parent: these Child objects can have different type of Parent so it easier to not know the parent (think of Child which represents Tags and parents can be differ...

nhibernate custom collection handling

Hello I have a working one to many relationship (NOT bbidirectional) where Resource has a set of many Allocations implented as shown below. The domain needs to do more with the collection of allocations that just manage it with AddAllocation, RemoveAllocation, etc. So from an object perspective, I'd like to put that extra logic that is ...

Why does LinkedHashSet<E> extend HashSet<e> and implement Set<E>

Opened a LinkedHashSet source code today and found some interesting thing: public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, java.io.Serializable { The question is: why do they need both "extends HashSet" and "implements Set" when HashSet already is the Set? ...

Java: Are there some tools out there which are able to refactor X[][] to List<List<X>>?

Hello, everyone! I have to refactor an existing project, which is not that small. It contains a lot of arrays declarations and accesses: (X is not a generic type, it's just a placeholder). declarations: X[] and X[][], access: someArray[i] and someArray[i][j]. I have to rewrite everything to use generic Lists: declaration: List<X> and L...

Is there a .NET collection interface that prevents adding objects?

I have a class that maintains list of objects of another class. List of objects is a public property. I would like to prevent users from adding and removing objects directly to list like this: MyObject.MyListProperty.Add(object); Instead I want them to use method that will internally do some processing and then add object to li...

Language Tricks to Shorten My Java Code?

I am currently rediscovering Java (working with Ruby a lot recently), and I love the compilation-time checking of everything. It makes refactoring so easy. However, I miss playing fast-and-loose with types to do an each loop. This is my worst code. Is this as short as it can be? I have a collection called looperTracks, which has instan...

java: returning a collection

What's the best way to return a collection in Java? Should I allow the caller to provide a collection to add to? Or just return a List<> or Set<> of items? Or both? public class Item { ... } public class SomeOtherClass { private List<Item> myItems; public List<Item> getItems() { return Collections.unmodifiableList(this.my...

Is IOrderedEnumerable<T> an inherently immutable collection? Should/can I yield?

I may not quite have my head around the IOrderedEnumerable<T> collection. If I have a field, my_field of type IOrderedEnumerable<T>, and I wish to return it from a public property getter, can I simply return my_field;? I'm used to yielding results to an IEnumerable<T> to prevent external modification to an internally held collection, bu...

What is the difference between google's ImmutableList and Collections.unmodifiableList ()?

From ImmutableList javadocs: Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that can still change, an instance of ImmutableList contains its own private data and will never change. ImmutableList is convenient for public static final lists ("constant lists") and also lets ...

Is it acceptable to return unmodifiableList or should I return array?

I have method List<Foo> getFoos () which gets the data from remote server and returns it. Of course, user shouldn't change number of items of the list because he'll get data not synchronized with data on the server (and if he want change number of items he has special methods like addFoo ()). First approach was to return array and cha...

Are there any collections in .NET that prevent null entries?

I'm specifically thinking about a collection that fulfills the contract of a set, but I think the question can apply to any kind. Are there collections in the .NET framework that prevent null entries? The specific behavior I want is this: var set = new HashSet<object>(); bool added = set.Add(null); Console.WriteLine(added); // prints "F...

linq populate custom collection

I have a collection defined as: public class MyCollection : List<MyItem> { ... } public class MyItem { ... } Using linq, I can use the Select method to return a IEnumerable, and I can call .ToList on that to get an IList but is there some way of getting back a type of MyCollection? Because I am tring to instantiate a class wh...

What is the collection version of SingleOrDefault for a Dictionary<T>?

Title kind of says it all. I just can't seem to find a DictionaryOrDefault \ ListOrDefault \ CollectionOrDefault option. Is there such a method? If not how do I do this: MyClass myObject = MyDictionary .SingleOrDefault(x => { if (x.Value != null) ...

Are there any java collection wrappers/collections that fail upon multithreaded access ?

I'm trying to track some concurrency problems related to collections in a large code-base. I would like to replace all the collections/maps with an alternate implementation that throws an exception when the 3rd thread accesses it (or similar; I can see several possible strategis that might work). Anyone know of any libraries/tools/strat...

Remove Duplicates from List of HashMap Entries

I have a List<HashMap<String,Object>> which represents a database where each list record is a database row. I have 10 columns in my database. There are several rows where the values of 2 particular columns are equals. I need to remove the duplicates from the list after the list is updated with all the rows from database. What is the ef...

Efficient Algorithm for comparing only the lists that have been updated

Hi everyone, Even describing this problem is hard, But I'll give it a go. I've been struggling with this for a few days and decided to ask here. OK, so I'm trying to model "concepts", or "things" as I call them. Just concepts in general. It's to do with processing logic. So, each "thing" is defined by it's relationship to other things...

Finding all the ICollectionView's attached to a collection

We have multiple filters based on the same collection. i.e. we are displaying the same collection in a variety of ways. What i would like to be able to do is ask all of the CollectionViews to refresh when a property changes (as the collection view will only refilter if items get added/removed from the collection). Is there a way to find ...

C++ How to store collection of templates objects regardless of tempate

I have a problem with implementing database table library. I have a class Column storing different types. template <class T> class Column : iColumn<T> { ... } Table is composed of columns, so I need a collection of them (map with string name as a key and column as value). How shall I implement one collection of all table's columns r...

Why isn't there a "set" interface in the .NET framework?

I'm thinking specifically about the generic class HashSet<T>. It implements several interfaces, but none exposes the correct semantics of a set. Specifically, none supports an Add method returning bool. (ICollection<T> supports void Add, which can be used in a pinch.) Also unsupported by these interfaces are common set operations like un...