collections

maximal element in an array in Java (Collections.max() for integer arrays int[])

Is there anything like Collections.max which finds the maximal value in an array for regular arrays in the standard java runtime library? ...

Quick Java question: Cloning and subtracting sets - does this work?

private HashMap<DataObject, HashSet> AllDataObjects; ... /** Returns all DataObject elements that are NOT in the specified set. */ private DataObject[] invert( HashSet<DataObject> set ) { HashSet<DataObject> keys = (HashSet) AllDataObjects.keySet(); keys = (HashSet) keys.clone(); keys.removeAll( set ); return (DataObj...

Quick Java question: Casting an array of Objects into an array of my intended class

Just for review, can someone quickly explain what prevents this from working (on compile): private HashSet Data; ... public DataObject[] getDataObjects( ) { return (DataObject[]) Data.toArray(); } ...and what makes this the way that DOES work: public DataObject[] getDataObjects( ) { return (DataObject[]) Data.toArray( new D...

Question about overzelaous implementation of IndexOf

I have a priority queue implementation in C# that I want to add a .IndexOf method to. However, since the priority queue doesn't really concern itself with the order of the values themselves (that is, if I were to just grab all the values, disregarding their priorities, they wouldn't necessarily have any order at all), only the priority ...

Checking for the existence of an object in a collection (of T)

I see that over on this question http://stackoverflow.com/questions/312024/linqy-way-to-check-if-any-objects-in-a-collection-have-the-same-property-value there is a request to say how to do something using LINQ to see if a property matches in a collection. However, is this the fastest reasonable process by which to do this? I will be dep...

What is the difference between List (of T) and Collection(of T)?

I've seen them used in a lot of the same ways, and I am worried I'm about to go down a path in design that is irreversible if I don't understand this better. Also, I am using .NET. ...

mySQL stored procedure with .NET connector problem

I've seen other topics that deal with this error but none that seem to correspond to my situation. First of all, my code works completely fine when i run it locally. But when i upload it to the server, i get the error: Parameter '?PuserName' not found in the collection. Here is the C# code: public DataSet GetEmployeeByUsername(strin...

Fastest way to compare two lists

I have a List (Foo) and I want to see if it's equal to another List (foo). What is the fastest way ? ...

What is the right way to initialize a non-empty static collection in C# 2.0?

I want to initialize a static collection within my C# class - something like this: public class Foo { private static readonly ICollection<string> g_collection = ??? } I'm not sure of the right way to do this; in Java I might do something like: private static final Collection<String> g_collection = Arrays.asList("A", "B"); is ther...

Strange behavior of Java's TreeMap put() method

I have the following code, which splits up a Vector into a string vector (to use as a key) and an integer at the end (to use as value). payoffs.put(new Vector<String>(keyAndOutput.subList(0, keyAndOutput.size() - 1)), Integer.parseInt(keyAndOutput.lastElement())); The TreeMap in question is constructed using a Comparator with the foll...

sorted collection in java

I'm a beginner in Java. Please suggest which collection(s) can/should be used for maintaining a sorted list in Java. I have tried Map and Set but they weren't what I was looking for. ...

Best way to use a List<T> in a Key/Value collection?

Hi, What's the best way to keep a collection-object (List in example) in a Key/Value situation, where the key is a ID and the value is a collection of a type T? Is this the only option or is there a better solution/another collection for this in .NET 3.5? var x = new Dictionary<int, List<type>>(); ...

when to use Collection<T> vs List<T>

What is the best practice for when to use one vs the other? Both implement IList<T> and hold the data in an ordered fashion, but only List expose the sorting semantics.... (edit: duplicate from here) ...

Is it a best practice to specify an initial capcity for collections?

A lot of the collection classes in .Net (i.e., List<T>, Dictionary<TKey, TValue>) have an overloaded constructor that lets you specify an initial capacity size. Is it a best practice to use this constructor? If so, is there a rule of them as to some kind of "Magic Number" you should use? It's one thing If I know the exact size ahead of t...

Binding ASP.Net Menu Control to a Collection

hi everyone, i am trying to use this code to bind my asp.net menu control to a collection.. but its giving me an error that my collection is now IHierarchyEnumerable.. which I understand why too.. StringCollection sc = pos.getAllmembers(); Menu1.DataSource = pos.getAllmembers().GetEnumerator(); is there a way around this.....

HashMap intialization parameters (load / initialcapacity)

What values should I pass to create an efficient HashMap / HashMap based structures for N items? In an ArrayList, the efficient number is N (N already assumes future grow). What should be the parameters for a HashMap? ((int)(N * 0.75d), 0.75d)? more? less? What is the effect of changing the load factor? ...

Best design to implement "Default" item in collection

For my example, I have a Person class with an arbitrary number of associated Addresses associated with it. So there will be an Addresses collection as a member of the Person class. In many applications which use the Person class, we will just want to retrieve the "Default" Address object. There are a few design questions regarding the...

How do send a Collection of Enums to a WCF Service

I am having trouble sending a collection of Enums to a WCF service method. I used this post as a guide: Sharing Enum with WCF Service [ServiceContract] [ServiceKnownType(typeof(MyEnum))] [ServiceKnownType(typeof(List<MyEnum>))] public interface IMyService{ [OperationContract] MyEnum ServiceMethod1( ); [OperationContract] ...

Removing objects from Java Collections

I have a HashMap (although I guess this question applies to other collections) of objects. From what I understand, when the documentation talks about removing mappings, then it is removing the entry from the hashtable, i.e. not necessarily destroying the actual object. If the only remaining reference to the object is in this table, then ...

Json to Map

Hi, What is the best way to convert a JSON code as this: { "data" : { "field1" : "value1", "field2" : "value2"}} in a Java Map in which one the keys are (field1, field2) and the values for those fields are (value1, value2). Any ideas? Should I use Json-lib for that? Or better if I write my own parser? Thanks in advance. ...