Map<Var, Integer = new LinkedHashMap<Var, Integer>();
//with Var my own object
I need a collection that:
- no permits duplicates; that is fine
- keep the order of insertion //fine too
- I can retrive an element by its position //no fine!
- I can retrive the value by the key //fine
WHich is the best? I thought arrayList and every time check if the new element I want insert within, is not within yet. The problem is that I must to many extraction by position, for this reason I thought it wasn't the best. actually I thinking to store them by Integer key instead of Var since even the Intger give me the position. The problem is that in other part of the program I'm accessign to it by the Var key
Last edited by mickey0; September 6th, 2010 at 06:26 AM.
You can't really compare a Map and a List as there are used for different purposes. How are you inserting your key and value into the List?
You need to decide what your primary actions on this collection are before trying to work out which one (or combination of them) to use.
If you are doing lots of insertion/removals other than just at the end of the list then using a LinkedHashMap is a good idea. If you mainly referencing objects by the value object then it isn't.
it's a map; with put I insert key; map.put (new Var(), new Integer(num));
Yes I can see that is a map but your original statement said "WHich is the best? I thought arrayList " so I asked how you were using the arraylist.
1. The primary operation is insert but now I realised that I must do the others operations I said too;
Ok, so use the LinkedHashMap. Determining which object type to use as the key is down to which one you want to ensure is unique as maps only test keys for duplicates. Now determine if the value object lookup is infrequent enough for an inefficient trawl through the map to not be a problem. Of course if the map is small this inefficiency is not really relevant.
If the value object lookup is so inefficient it really is a problem and I mean only after using profiling tools to prove it's an issue there are ways around it.
Another problem: I overrode hashCode and equals of MyObject to put the value inside a
The code you have shown is confusing and wrong. If you are going to post code make sure it compiles and clearly shows the point you are trying to make.
The first code snippet has typos in it and the generics are for a map but you are creating a set.
The code for MyObject doesn't have overridden hashcode and equals methods
How do these code snippets relate to each other?
are for me identic; so I don't want put them twice withint myset; more or less I think I need two version of equals and hasCode.
Is there a way to solve this?
I've no idea what you are talking about. Give all the classes involved sensible names, clearly explain which class is in which collection, and what makes two instances of a class equal.