collections

Is there a way to get the value of a HashMap randomly in Java?

Is there a way to get the value of a HashMap randomly in Java? ...

When should I return the Interface and when the concrete class?

Hello SO people, when programming in Java I practically always, just out of habit, write something like this: public List<String> foo() { return new ArrayList<String>(); } Most of the time without even thinking about it. Now, the question is: should I always specify the interface as the return type? Or is it advisable to use the ...

How do you cast a List of objects from one type to another in Java?

For example, lets say you have two classes: public class TestA {} public class TestB extends TestA{} I have a method that returns a List<TestA> and I would like to cast all the objects in that list to TestB so that I'd end up with List<TestB>. ...

How to use LINQ to retrieve child collection

Starting here: public class Customer { public int CustomerID { get; set; } public string CustomerName { get; set; } public IList<Order> Orders { get; set; } } public class Order { public int OrderID { get; set; } public int CustomerID { get; set; } } What would be the linq query that you write to retrieve all Orders from ...

Using Generics in Interfaces

How do I allow my CookieData to be generic in the following code? I get an compile-time error on the declaration of ICookieService2. public struct CookieData<T> { T Value { get; set; } DateTime Expires { get; set; } } public interface ICookieService2: IDictionary<string, CookieData<T>> { // ... } My error is: The type...

C#:: When to use events or a collection of objects derived from an event handling Interface?

I have what I think is a simple "problem" to which I have found a couple of solutions but I am not sure which way to go andn the best practice in C#. I have a master object (say a singleton) instanciated once during the lifespan of the application. This "MasterClass" creates a bunch of new type of objects, say "SlaveClass" every time Ma...

best collection object for FIFO and ability to view items by position

I'm looking for a collection object/strategy that can allow for FIFO and lets me view the items in the collection by simply specifying their position. To clarify: I would like this data structure to hold say 100 DTO objects, and then when it gets to 101, I can make room by deleting the first item, etc. (FIFO). I'd like to be able to r...

Changing the width of PropertyGrid left-side collection editor/view

Anything with a long string simply introduces an unusable view with a scrollbar.. Is the width on collection editor fixed by the-design and can a splitter be introduced to this awesome presentation? ...

Subsonic custom collection type

Hi guys, This may seem like a simple question however i've spent the last hour trying to solve this. I want to create a custom subsonic collection so that i can fill it with data from a query with multiple joins using the .ExecuteAsCollection<>(); method. i've created the custom class and a custom collection and even a controller with...

How do I map a nested collection, Map<Key,List<Values>>, with hibernate JPA annotations?

I have a class I am not sure how to annotate properly. My goal for Holder::data: List should maintain order not by comparator but by the natural ordering of the elements in the array. (Which can be an ndx column if that is helpful.) Holder will have the only reference to data, so Cascade all is probably applicable as well. I am al...

How to create object collection from an xml

I have an xml like the following <DataCollection> <Data> <Name>John</Name> <Age>30</Age> </Data> ... more person </DataCollection> I want to create an object(or some type) collection with Name, Age as fields. One problem that I have is I don't know the structure before hand (all i know is, there will be DataCollection an...

How to convert .net collection type to VBA collection?

I am writing a VBA application which calls to C#.net method which returns collection object. When I assign the output to VBA.Collection I get a compile time error - Argument not optional. If I assign the output to Object I get run time error - Object variable or With block variable not set I think it is because of incompatible Collect...

different using. java.util.Enumeration and Iterator

what is the exact different of both.. is using enumeration more benefit than using iterator..? can anyone elaborate.. any reference article would be appeciated ...

scala collection.Map cannot be added to

Why can't I add to a scala.collection.Map? It seems that this trait is pretty useless without this functionality. Couldn't they have overridden the ++ method in Iterable and reduced the return type to a Map? P.S. I don't mean that it should be mutable, just that it should be able to return a new Map with an added mapping (or mappings)...

Sorting an collecton of java beans by field

Hi, I have an collection of java beans that populate a JSF DataTable I'm trying to implement column sorting. I'd like to sort the array/collection depending on the field selected. I have using Reflection in the past for this, but wanted to find a neater way of doing it, using Commons BeanUtils and/or Collections but can't seem to find...

Passing and processing List<> types containing objects of the same base type.

Consider the following classes: class TypeA; class TypeB : TypeA; class TypeC : TypeA; class TypeD : TypeA; and the following List<> types: List<TypeB> listTypeB; List<TypeC> listTypeC; List<TypeD> listTypeD; Now TypeA has a property Prop1 of type Object1 and I want to locate which list has stored within it an item with Prop1 of a ...

How to implement a list fold in Java

I have a List and want to reduce it to a single value (functional programming term "fold", Ruby term inject), like Arrays.asList("a", "b", "c") ... fold ... "a,b,c" As I am infected with functional programming ideas (Scala), I am looking for an easier/shorter way to code it than sb = new StringBuilder for ... { append ... } sb.toS...

What's wrong with Collections.sort?

I have these strings in an ArrayList of String in no particular order but when I invoke Collections.sort(listReference), the sorted result is incorrect, why do 10 and 11 (the last 2 characters) come before 07, 08, 09? 12880 20090506054200001 12880 20090506054200002 12880 20090513070200003 12880 20090513070200004 12880 2009052020260...

Can you use C++ templates to specify a collection type and the specialization of that type?

Example, I want to specialize a class to have a member variable that is an stl container, say a vector or a list, so I need something like: template <class CollectionType, class ItemType> class Test { public: CollectionType<ItemType> m_collection; }; So I can do: Test t = Test<vector, int>(); t.m_collection<vector<int>> = vector...

Number of elements in a javascript object

Is there a way to get (from somewhere) the number of elements in a javascript object?? (i.e. constant-time complexity). I cant find a property or method that retrieve that information. So far I can only think of doing an iteration through the whole collection, but that's linear time. It's strange there is no direct access to the size of...