There is no difference between the objects; you have a HashMap<String, Object> in both cases. There is a difference in the interface you have to the object. In the first case, the interface is HashMap<String, Object>, whereas in the second it's Map<String, Object>. But the underlying object is the same.

The advantage to using Map<String, Object> is that you can change the underlying object to be a different kind of map without breaking your contract with any code that's using it. If you declare it as HashMap<String, Object>, you have to change your contract if you want to change the underlying implementation.


Example: Let's say I write this class:

class Foo {
    private HashMap<String, Object> things;
    private HashMap<String, Object> moreThings;

    protected HashMap<String, Object> getThings() {
        return this.things;
    }

    protected HashMap<String, Object> getMoreThings() {
        return this.moreThings;
    }

    public Foo() {
        this.things = new HashMap<String, Object>();
        this.moreThings = new HashMap<String, Object>();
    }

    // ...more...
}

The class has a couple of internal maps of string->object which it shares (via accessor methods) with subclasses. Let's say I write it with HashMaps to start with because I think that's the appropriate structure to use when writing the class.

Later, Mary writes code subclassing it. She has something she needs to do with both things and moreThings, so naturally she puts that in a common method, and she uses the same type I used on getThings/getMoreThings when defining her method:

class SpecialFoo extends Foo {
    private void doSomething(HashMap<String, Object> t) {
        // ...
    }

    public void whatever() {
        this.doSomething(this.getThings());
        this.doSomething(this.getMoreThings());
    }

    // ...more...
}

Later, I decide that actually, it's better if I use TreeMap instead of HashMap in Foo. I update Foo, changing HashMap to TreeMap. Now, SpecialFoo doesn't compile anymore, because I've broken the contract: Foo used to say it provided HashMaps, but now it's providing TreeMaps instead. So we have to fix SpecialFoo now (and this kind of thing can ripple through a codebase).

Unless I had a really good reason for sharing that my implementation was using a HashMap (and that does happen), what I should have done was declare getThings and getMoreThings as just returning Map<String, Object> without being any more specific than that. In fact, barring a good reason to do something else, even within Foo I should probably declare things and moreThings as Map, not HashMap/TreeMap:

class Foo {
    private Map<String, Object> things;             // <== Changed
    private Map<String, Object> moreThings;         // <== Changed

    protected Map<String, Object> getThings() {     // <== Changed
        return this.things;
    }

    protected Map<String, Object> getMoreThings() { // <== Changed
        return this.moreThings;
    }

    public Foo() {
        this.things = new HashMap<String, Object>();
        this.moreThings = new HashMap<String, Object>();
    }

    // ...more...
}

Note how I'm now using Map<String, Object> everywhere I can, only being specific when I create the actual objects.

If I had done that, then Mary would have done this:

class SpecialFoo extends Foo {
    private void doSomething(Map<String, Object> t) { // <== Changed
        // ...
    }

    public void whatever() {
        this.doSomething(this.getThings());
        this.doSomething(this.getMoreThings());
    }
}

...and changing Foo wouldn't have made SpecialFoo stop compiling.

Interfaces (and base classes) let us reveal only as much as is necessary, keeping our flexibility under the covers to make changes as appropriate. In general, we want to have our references be as basic as possible. If we don't need to know it's a HashMap, just call it a Map.

This isn't a blind rule, but in general, coding to the most general interface is going to be less brittle than coding to something more specific. If I'd remembered that, I wouldn't have created a Foo that set Mary up for failure with SpecialFoo. If Mary had remembered that, then even though I messed up Foo, she would have declared her private method with Map instead of HashMap and my changing Foo's contract wouldn't have impacted her code.

Sometimes you can't do that, sometimes you have to be specific. But unless you have a reason to be, err toward the least-specific interface.

Answer from T.J. Crowder on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › HashMap.html
HashMap (Java Platform SE 8 )
2 weeks ago - Java™ Platform Standard Ed. 8 ... Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits ...
🌐
W3Schools
w3schools.com › java › java_hashmap.asp
Java HashMap
It is part of the java.util package and implements the Map interface. Instead of accessing elements by an index (like with ArrayList), you use a key to retrieve its associated value. A HashMap can store many different combinations, such as:
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-hashmap-in-java-with-examples
HashMap in Java - GeeksforGeeks
A HashMap is a part of Java’s Collection Framework and implements the Map interface. It stores elements in key-value pairs, where, Keys are unique.
Published   November 27, 2025
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-hashmap
Java HashMap - HashMap in Java | DigitalOcean
August 4, 2022 - So HashMap key object should provide good implementation of these methods. This is the reason immutable classes are better suitable for keys, for example String and Interger. Java HashMap is not thread safe, for multithreaded environment you should use ConcurrentHashMap class or get synchronized map using Collections.synchronizedMap() method.
🌐
Turing
turing.com › kb › implementing-hashmap-in-java
How to Implement HashMap in Java from Scratch
The HashMap works by using a special code (hash function) to save the information in an organized way, like putting things in labeled boxes. Sometimes, different words might end up in the same box, but the HashMap keeps them separate using a ...
Top answer
1 of 13
573

There is no difference between the objects; you have a HashMap<String, Object> in both cases. There is a difference in the interface you have to the object. In the first case, the interface is HashMap<String, Object>, whereas in the second it's Map<String, Object>. But the underlying object is the same.

The advantage to using Map<String, Object> is that you can change the underlying object to be a different kind of map without breaking your contract with any code that's using it. If you declare it as HashMap<String, Object>, you have to change your contract if you want to change the underlying implementation.


Example: Let's say I write this class:

class Foo {
    private HashMap<String, Object> things;
    private HashMap<String, Object> moreThings;

    protected HashMap<String, Object> getThings() {
        return this.things;
    }

    protected HashMap<String, Object> getMoreThings() {
        return this.moreThings;
    }

    public Foo() {
        this.things = new HashMap<String, Object>();
        this.moreThings = new HashMap<String, Object>();
    }

    // ...more...
}

The class has a couple of internal maps of string->object which it shares (via accessor methods) with subclasses. Let's say I write it with HashMaps to start with because I think that's the appropriate structure to use when writing the class.

Later, Mary writes code subclassing it. She has something she needs to do with both things and moreThings, so naturally she puts that in a common method, and she uses the same type I used on getThings/getMoreThings when defining her method:

class SpecialFoo extends Foo {
    private void doSomething(HashMap<String, Object> t) {
        // ...
    }

    public void whatever() {
        this.doSomething(this.getThings());
        this.doSomething(this.getMoreThings());
    }

    // ...more...
}

Later, I decide that actually, it's better if I use TreeMap instead of HashMap in Foo. I update Foo, changing HashMap to TreeMap. Now, SpecialFoo doesn't compile anymore, because I've broken the contract: Foo used to say it provided HashMaps, but now it's providing TreeMaps instead. So we have to fix SpecialFoo now (and this kind of thing can ripple through a codebase).

Unless I had a really good reason for sharing that my implementation was using a HashMap (and that does happen), what I should have done was declare getThings and getMoreThings as just returning Map<String, Object> without being any more specific than that. In fact, barring a good reason to do something else, even within Foo I should probably declare things and moreThings as Map, not HashMap/TreeMap:

class Foo {
    private Map<String, Object> things;             // <== Changed
    private Map<String, Object> moreThings;         // <== Changed

    protected Map<String, Object> getThings() {     // <== Changed
        return this.things;
    }

    protected Map<String, Object> getMoreThings() { // <== Changed
        return this.moreThings;
    }

    public Foo() {
        this.things = new HashMap<String, Object>();
        this.moreThings = new HashMap<String, Object>();
    }

    // ...more...
}

Note how I'm now using Map<String, Object> everywhere I can, only being specific when I create the actual objects.

If I had done that, then Mary would have done this:

class SpecialFoo extends Foo {
    private void doSomething(Map<String, Object> t) { // <== Changed
        // ...
    }

    public void whatever() {
        this.doSomething(this.getThings());
        this.doSomething(this.getMoreThings());
    }
}

...and changing Foo wouldn't have made SpecialFoo stop compiling.

Interfaces (and base classes) let us reveal only as much as is necessary, keeping our flexibility under the covers to make changes as appropriate. In general, we want to have our references be as basic as possible. If we don't need to know it's a HashMap, just call it a Map.

This isn't a blind rule, but in general, coding to the most general interface is going to be less brittle than coding to something more specific. If I'd remembered that, I wouldn't have created a Foo that set Mary up for failure with SpecialFoo. If Mary had remembered that, then even though I messed up Foo, she would have declared her private method with Map instead of HashMap and my changing Foo's contract wouldn't have impacted her code.

Sometimes you can't do that, sometimes you have to be specific. But unless you have a reason to be, err toward the least-specific interface.

2 of 13
64

Map is an interface that HashMap implements. The difference is that in the second implementation your reference to the HashMap will only allow the use of functions defined in the Map interface, while the first will allow the use of any public functions in HashMap (which includes the Map interface).

It will probably make more sense if you read Sun's interface tutorial

Find elsewhere
🌐
W3Schools
w3schools.com › java › java_ref_hashmap.asp
Java HashMap Reference
Some methods use the type of the HashMap's entries as a parameter or return value. The type of the key will be referred to as K and the type of the value will be referred to as V in the table. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Educative
educative.io › blog › what-is-a-hashmap-in-java
What is a HashMap in Java?
3 weeks ago - This blog provides a comprehensive guide to Java HashMaps, explaining their key-value pair storage, internal workings using hash tables and linked lists, and methods like put(), get(), and remove(). It highlights HashMap's efficiency in data retrieval and management but notes its limitations, like lack of inherent order and thread safety.
🌐
Edureka
edureka.co › blog › java-hashmap
Java HashMap: Implementing HashMap in Java with Examples | Edureka
July 26, 2023 - A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. ... HashMap is a Map-based collection class in Java which is used to store data in Key & Value pairs. It also helps in implementing the Map interface in Java.
🌐
Codecademy
codecademy.com › docs › java › hashmap
Java | HashMap | Codecademy
May 17, 2025 - A HashMap is an important part of the Java collections framework and implements the Map interface. It stores items as key-value pairs, where each key is unique and is used to fetch the associated value.
🌐
Medium
rameshfadatare.medium.com › java-hashmap-e8aef2a24b8c
Java HashMap. Welcome to the Java Collections… | by Ramesh Fadatare | Medium
September 21, 2024 - HashMap in Java is part of the Java Collections Framework and implements the Map interface. It is a hash table-based implementation that provides the ability to store and retrieve key-value pairs.
🌐
Built In
builtin.com › articles › hashmap-in-java
A Guide to HashMap in Java With Examples | Built In
HashMap is a popular data structure in Java that uses the Map interface and a hash table to provide efficient access and manipulation of data based unique keys.
🌐
Medium
medium.com › @yashodhara.chowkar › internal-working-of-hashmap-in-java-and-performance-improvement-in-java-8-a28ee1660cda
Internal Working of HashMap in Java and Performance Improvement in Java 8 | by Yashodhara C H | Medium
May 14, 2024 - In Java 8, when the number of nodes in a single bucket reaches a threshold called Treeify Threshold the HashMap converts the internal structure of that bucket from a linked list to a Tree data structure. All node within the bucket are converted into TreeNode.
🌐
DataCamp
datacamp.com › doc › java › hashmap
Java HashMap
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... A HashMap in Java is a part of the Java Collections Framework that implements the Map interface. It is used to store key-value pairs and provides constant-time performance ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › hashmap-class-methods-java-examples-set-1-put-get-isempty-size
HashMap Class Methods in Java with Examples
July 23, 2025 - HashMap stores key value pairs (for example records of students as value and roll number as key) and HashSet stores only keys (for example a set if integers). ... Please refer HashSet in Java for details.
🌐
Reddit
reddit.com › r/java › can someone explain to me like i'm 5 what a hashmap is and how to basically use it? thanks.
r/java on Reddit: Can someone explain to me like I'm 5 what a hashmap is and how to basically use it? Thanks.
March 12, 2013 - The java.util.Map spec states that the value associated to that key is overriden. HashMap comes from Java2 collections and thus it was born a Map.
🌐
freeCodeCamp
freecodecamp.org › news › how-java-hashmaps-work-internal-mechanics-explained
How Java HashMaps Work – Internal Mechanics Explained
August 9, 2024 - Hashing is a technique that transforms an input of arbitrary size into a fixed-size output using a hash function. The generated output is called the hash code and is represented by an integer value in Java. Hash codes are used for efficient lookup and storage operations in a HashMap.
🌐
HackerNoon
hackernoon.com › what-is-a-hashmap-in-java
What is a HashMap in Java? | HackerNoon
January 10, 2022 - Hashmap is a collection of key-value pairs and an array of nodes. It uses an array and LinkedList for storing key-value pairs.
🌐
Quora
quora.com › What-is-HashMap-in-Java-with-examples
What is HashMap in Java with examples? - Quora
Answer: HashMap is a part of java.util.package and it is for implementing hashing in java. Suppose I give you and array like - {1,2,1,3,4,5,6,7,8,8,1,1,1,9,9} and now I ask you the count of each element of the array, then what would be the naive approach?…to iterate through the array for each ele...