What is the difference between the following maps I create (in another question, people answered using them seemingly interchangeably and I\'m wondering if/how they are diff
Map has the following implementations:
HashMap Map m = new HashMap();
LinkedHashMap Map m = new LinkedHashMap();
Tree Map Map m = new TreeMap();
WeakHashMap Map m = new WeakHashMap();
Suppose you have created one method (this is just pseudocode).
public void HashMap getMap(){
return map;
}
Suppose your project requirements change:
HashMap
. HashMap
to LinkedHashMap
. LinkedHashMap
to TreeMap
. If your method returns specific classes instead of something that implements the Map
interface, you have to change the return type of getMap()
method each time.
But if you use the polymorphism feature of Java, and instead of returning specific classes, use the interface Map
, it improves code reusability and reduces the impact of requirement changes.