HOWTO · Java

Difference Between Hashmap and Map in Java

This article introduces the difference between hashmap and map interface in Java.

On this page

Map is a Java interface that describes operations on key-value associations. HashMap is a concrete class that implements that interface. They are therefore different kinds of types: Map defines what callers can do, while HashMap supplies one way to do it. Other standard implementations include TreeMap and LinkedHashMap.

Map is an interface; HashMap is an implementation

An interface cannot be instantiated directly. A declaration such as Map<String, Integer> scores names the operations the variable can use; an object such as new HashMap<>() provides the implementation. The generic types say that keys are strings and values are integers. Calling put, get, or containsKey through the Map reference works without tying the rest of the code to HashMap internals.

The distinction also explains why this assignment is valid: HashMap<String, Integer> scores = new HashMap<>(); The variable and object have the same concrete type. You can instead declare the variable as Map<String, Integer> and still construct a HashMap. Prefer the interface when your code needs only the operations promised by Map; this keeps the choice of implementation local and makes later substitutions easier.

Choose the Map reference or a concrete type

Use a Map reference for fields, local variables, return types, and parameters when callers only need map operations. For example, a method that accepts Map<String, Integer> can receive a compatible HashMap, TreeMap, or LinkedHashMap. The method then depends on the shared interface rather than a particular storage class.

Use a concrete type when construction or later operations specifically require that implementation. For example, choose TreeMap when you need keys kept in their natural or comparator-defined sorted order. A concrete declaration may also make a deliberate implementation choice clear, but it narrows which replacement objects can be assigned to that variable. The Map interface does not expose every implementation-specific operation.

How Map implementations differ in behavior

The interface defines common map operations; it does not promise one iteration order for all implementations. HashMap does not guarantee a stable iteration order. Do not write code or tests that rely on the order in which its entries happen to print. TreeMap iterates according to key order, while LinkedHashMap maintains encounter order (normally insertion order for the usual constructor). These choices affect traversal and displayed results, not the basic Map operations.

This is why a Map-typed variable does not make different implementations behaviorally identical. Choose the implementation whose documented behavior suits the task, then use the interface for code that should remain independent of that choice. If ordering matters, state and select the ordering policy explicitly instead of depending on the current output of a HashMap.

Use Map in method parameters

Accept Map<K, V> in a method when it needs only map operations. This lets the caller decide which compatible implementation to provide, and avoids duplicate overloads for HashMap, TreeMap, and LinkedHashMap. The method can still observe implementation-specific iteration behavior, so it should avoid promising an order unless its contract enforces one.

Run a complete Java example

The following self-contained program passes both a HashMap and a LinkedHashMap to a method whose parameter type is Map, then copies entries into a TreeMap. It looks up one value through the interface and traverses the ordered maps to show their different ordering policies. Save it as Main.java, compile with javac Main.java, and run with java Main. The supplied packet has no execution result for this code, so this article does not assert observed output.

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    private static void printValue(Map<String, Integer> values, String key) {
        System.out.println(key + "=" + values.get(key));
    }

    private static void printEntries(Map<String, Integer> values) {
        for (Map.Entry<String, Integer> entry : values.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }

    public static void main(String[] args) {
        Map<String, Integer> hashValues = new HashMap<>();
        hashValues.put("pear", 3);
        hashValues.put("apple", 5);
        printValue(hashValues, "pear");

        Map<String, Integer> insertionOrder = new LinkedHashMap<>();
        insertionOrder.put("pear", 3);
        insertionOrder.put("apple", 5);

        printEntries(insertionOrder);
        Map<String, Integer> sorted = new TreeMap<>(insertionOrder);
        printEntries(sorted);
    }
}