In my code I default to using ArrayList for all Lists, HashMap for all maps, HashSet for all sets.
From a practical standpoint how much am I losing in flexibility, scalability, readability and performance by choosing the wrong implementation? When does it make sense to spend time to decide to use one rather than another?
I certainly see a very clear cut case for why someone would use a LinkedList instead of an ArrayList given certain circumstances. When does someone feel that it is critical they use a HashMap rather than a TreeMap or a HashTable? What about Sets?
Questions:
I've read through:
I found this question to be related from a theoretical point of view, but I'm more interested in a real world, down in the trenches answer.
This is a very general question, but i´ll throw in a couple of thougts.
If you are programming oriented to interfaces, then flexibility wont take a great hit. For example
void foo(List<E> list);
The cost of choosing poorly could be seen in performance penalties. For example, choosing a LinkedList when direct access (as in ArrayList) is what you are looking for.
Sets have a similiar issue. If you want to keep sorted collections with no duplicates, a SortedSet would be a wiser choice over a HashSet. In the latter one, you´d have to sort the entire Set manually (this is, a call to Collections.sort())
<EDIT>
As for maps, there are a lot of different implementations. Each one has a different purpose. For example, there´s SortedMap, analog to SortedSet. Then, there´s WeakHashMap, that doesn´t work like a HashMap, in the sense that keys can be removed by the garbage collector. As you can imagine, the choice between a HashMap and a WeakHashMap is not trivial. As always, depends on what you want to implement with them.
</EDIT>
Regarding the story, in my current project, we replaced a HashSet with a SortedSet because performance was being affected. DataCenter didn´t caught fire though.
My two cents.
So long as you follow the good OO practice of depending on an abstract type, what does it matter?
If for example you find you've used the wrong Map
you simply change the implementation you are using and because all of your dependencies are on Map
everything works as before just with the different performance characteristics.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With