What is the difference between the HashMap and Map objects in Java?

后端 未结 13 1169
无人及你
无人及你 2020-11-22 17:07

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

相关标签:
13条回答
  • 2020-11-22 17:19

    enter image description here

    Map has the following implementations:

    1. HashMap Map m = new HashMap();

    2. LinkedHashMap Map m = new LinkedHashMap();

    3. Tree Map Map m = new TreeMap();

    4. 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:

    1. The method should return map contents - Need to return HashMap.
    2. The method should return map key's in insertion order - Need to change return type HashMap to LinkedHashMap.
    3. The method should return map key's in sorted order - Need to change return type 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.

    0 讨论(0)
  • 2020-11-22 17:21

    Adding to the top voted answer and many ones above stressing the "more generic, better", I would like to dig a little bit more.

    Map is the structure contract while HashMap is an implementation providing its own methods to deal with different real problems: how to calculate index, what is the capacity and how to increment it, how to insert, how to keep the index unique, etc.

    Let's look into the source code:

    In Map we have the method of containsKey(Object key):

    boolean containsKey(Object key);
    

    JavaDoc:

    boolean java.util.Map.containsValue(Object value)

    Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of the Map interface.

    Parameters:value

    value whose presence in this map is to betested

    Returns:true

    if this map maps one or more keys to the specified

    valueThrows:

    ClassCastException - if the value is of an inappropriate type for this map (optional)

    NullPointerException - if the specified value is null and this map does not permit null values (optional)

    It requires its implementations to implement it, but the "how to" is at its freedom, only to ensure it returns correct.

    In HashMap:

    public boolean containsKey(Object key) {
        return getNode(hash(key), key) != null;
    }
    

    It turns out that HashMap uses hashcode to test if this map contains the key. So it has the benefit of hash algorithm.

    0 讨论(0)
  • 2020-11-22 17:22

    Map is interface and Hashmap is a class that implements Map Interface

    0 讨论(0)
  • 2020-11-22 17:23

    Map is the static type of map, while HashMap is the dynamic type of map. This means that the compiler will treat your map object as being one of type Map, even though at runtime, it may point to any subtype of it.

    This practice of programming against interfaces instead of implementations has the added benefit of remaining flexible: You can for instance replace the dynamic type of map at runtime, as long as it is a subtype of Map (e.g. LinkedHashMap), and change the map's behavior on the fly.

    A good rule of thumb is to remain as abstract as possible on the API level: If for instance a method you are programming must work on maps, then it's sufficient to declare a parameter as Map instead of the stricter (because less abstract) HashMap type. That way, the consumer of your API can be flexible about what kind of Map implementation they want to pass to your method.

    0 讨论(0)
  • 2020-11-22 17:24

    HashMap is an implementation of Map so it's quite the same but has "clone()" method as i see in reference guide))

    0 讨论(0)
  • 2020-11-22 17:24
    HashMap<String, Object> map1 = new HashMap<String, Object>();
    Map<String, Object> map2 = new HashMap<String, Object>();  
    

    First of all Map is an interface it has different implementation like - HashMap, TreeHashMap, LinkedHashMap etc. Interface works like a super class for the implementing class. So according to OOP's rule any concrete class that implements Map is a Map also. That means we can assign/put any HashMap type variable to a Map type variable without any type of casting.

    In this case we can assign map1 to map2 without any casting or any losing of data -

    map2 = map1
    
    0 讨论(0)
提交回复
热议问题