🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › HashMap.html
HashMap (Java SE 21 & JDK 21)
January 20, 2026 - 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 nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
🌐
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
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › util › HashMap.java
jdk/src/java.base/share/classes/java/util/HashMap.java at master · openjdk/jdk
import java.util.function.Function; import jdk.internal.access.SharedSecrets; · /** * Hash table based implementation of the {@code Map} interface. This · * implementation provides all of the optional map operations, and permits · * {@code null} values and the {@code null} key. (The {@code HashMap} * class is roughly equivalent to {@code Hashtable}, except that it is ·
Author   openjdk
🌐
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.
🌐
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 ...
Find elsewhere
🌐
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

🌐
Baeldung
baeldung.com › home › java › java collections › java map › a guide to java hashmap
A Guide to Java HashMap | Baeldung
January 15, 2025 - The above test case may be surprising if we don’t have a good understanding of how HashMap works internally. For this to work correctly, equal keys must have the same hash, however, different keys can have the same hash. If two different keys have the same hash, the two values belonging to them will be stored in the same bucket. Inside a bucket, values are stored in a list and retrieved by looping over all elements. The cost of this is O(n). As of Java 8 (see JEP 180), the data structure in which the values inside one bucket are stored is changed from a list to a balanced tree if a bucket contains 8 or more values, and it’s changed back to a list if, at some point, only 6 values are left in the bucket.
🌐
Programiz
programiz.com › java-programming › hashmap
Java HashMap (With Examples)
Become a certified Java programmer. Try Programiz PRO! ... The HashMap class of the Java collections framework provides the functionality of the hash table data structure.
🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › HashMap.html
HashMap (Java SE 11 & JDK 11 )
January 20, 2026 - 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 nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
🌐
GeeksforGeeks
geeksforgeeks.org › java › hashmap-class-methods-java-examples-set-1-put-get-isempty-size
HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size()) - GeeksforGeeks
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.
🌐
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.
🌐
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.
🌐
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.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › hashmap-java
HashMap in Java: A Comprehensive Guide
HashMap in Java falls under the most commonly used data structures today. It is basically a collection based on Map that holds key-value pairs. They are used in Java when distinct keys are available for data that we aim to store.