Hi,
I saw in many places that people use:
Map m= new HashMap();
The best reason I can offer here is that this should tell a maintence
programmer:
* The HashMap implementation is not strictly required
* Defer to documentation for implicit assumptions required of the
implementation
Eg, if you were to instead say
import java.util.concurrent.*;
ConcurrentMap m=new ConcurrentHashMap()
This tells the user that the choice of a Hash'ed map is not strictly
required, but the map must be safe for access by other threads. It also
implies that callers should be safe to iterate through m.entrySet(), but
those methods should beware that the iterator could return entries that
did not exist when the iterator was created, or might not return entries
that did exist when the iterator was created.
Likewise, using
SortedMap m=new TreeMap()
tells the reader that your algorithm implicitly requires that entries be
sorted, and may fail spuriously if the implementation changes.
In the end,
Map m=new HashMap()
This says that, in the future, this algorithm will still work correctly if
the keys are converted to Enums and the map is replaced with the faster
EnumMap implementation as
Map m=new EnumMap(MyEnum.class);
However, the same programmer should really read the documentation and
scratch their head before attempting to refit this code with
Collections.synchronizedMap, Collections.unmodifiableMap, an
implementation of ConcurrentMap, etc.
HTH,
-Zig