🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › HashMap.html
HashMap (Java Platform SE 8 )
3 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
🌐
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
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-hashmap
Java HashMap - HashMap in Java | DigitalOcean
August 4, 2022 - Note that HashMap capacity is always power of 2. Default load factor of 0.75 provides good tradeoff between space and time complexity. But you can set it to different values based on your requirement. If you want to save space, then you can increase it’s value to 0.80 or 0.90 but then get/put operations will take more time. Java HashMap keySet method returns the Set view of keys in the HashMap.
🌐
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.
🌐
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.
🌐
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
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java collections › java map › a guide to java hashmap
A Guide to Java HashMap | Baeldung
January 16, 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.
🌐
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 ...
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

🌐
Great Learning
mygreatlearning.com › blog › it/software development › hashmap in java: a complete guide
HashMap in Java: A Complete Guide
February 13, 2025 - Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry. ... Internally, HashMap in Java uses an array of linked lists (known as a bucket array). The key’s hash code determines which bucket it will be placed into.
🌐
Built In
builtin.com › articles › hashmap-in-java
A Guide to HashMap in Java With Examples | Built In
January 18, 2024 - 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.
🌐
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.
🌐
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 ...
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › java_hashmap_class.htm
Java - The HashMap Class
Apart from the methods inherited from its parent classes, HashMap defines the following methods − · The following program illustrates several of the methods supported by this collection − · import java.util.*; public class HashMapDemo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Zara", new Double(3434.34)); hm.put("Mahnaz", new Double(123.22)); hm.put("Ayan", new Double(1378.00)); hm.put("Daisy", new Double(99.22)); hm.put("Qadir", new Double(-19.08)); // Get a set of the entries Set set = hm.entrySet(); //
🌐
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.
🌐
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.
🌐
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.