[java] HasSet vs ArrayList and modelling issue
Hello,
I must implement a class MyMethod; read a method declaration from a file with a parser, and while parsing I must fill that MyMethod Object.
Now, at the moment this my objects are in this fashion:
Code:
//allow me all public to reduce code verbosity
class MyMethod {
public Signature s;
public String returnedType;
}
class MySignature {
public String name;
//public List<MyVar> vars = new ArrayList<Var>();
public Set<MyVar> vars = new LinkedHashSet<MyVar>(); //eg, (int x, String s, int y)'
}
class MyVar { //eg. 'int x'
public String id;
public String type;
}
Now, in MySignature I used a Linked hashSet because I want to keep the order of insertion (and I know that I can't have two params with the same ID, here overrivde properly) since the Parser parser the list of parameters in the order they are written.
I want to keep a position field because I would like, given an id (eg, 'x'), find its position within a certain signature WITHOUT iterate over all list of parameters (I suppose this is slower). So:
- was it better to iterate on it (and better declare it as ArrayList) ? using arrayList I could avoid the position "problem", since I can retrieve it from iterating on the List.
- assuming to really need the position, where could I put it ? Within MyVar?
- assuming to use the Set, how can I override equals in MyVar to cope this situation (ie. no at most one MyVar with a certain Id)
thanks.
Re: [java] HasSet vs ArrayList and modelling issue
It's impossible to give a valid answer without knowing how many items might be in the list. If it's small (a few hundred items or less) then you would be hard pressed to see any difference in throughput no matter which container you chose. Processing times really only come into play when containers get quite large, with thousands of items.
If you need to order your items in a container that isn't sequential, you can provide a comparison operator that allows the container to sort the items according to your own criteria, and/or you can add a field to your item class that keeps track of the position it would hold in a sequential list.
Re: [java] HasSet vs ArrayList and modelling issue
Quote:
Originally Posted by
SixDegrees
It's impossible to give a valid answer without knowing how many items might be in the list. If it's small (a few hundred items or less) then you would be hard pressed to see any difference in throughput no matter which container you chose. Processing times really only come into play when containers get quite large, with thousands of items.
If you need to order your items in a container that isn't sequential, you can provide a comparison operator that allows the container to sort the items according to your own criteria, and/or you can add a field to your item class that keeps track of the position it would hold in a sequential list.
Hello,
my problem is indeed that. the list/set of params are the normal number of params of a function: zero, one, two, even 10; I don't even know what I need; in fact the question was even on that. What I need? How do you model this? Which container would you choose?