Java Tutorial

Java Map

A Map stores key → value pairs. put writes, get reads, and keys are unique — the second put for the same key replaces the value.

put and get

HashMap is the usual implementation. Declare the variable as Map<K, V>. Missing keys return null from get (or use getOrDefault).

Example

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map ages = new HashMap<>();
    ages.put("Ada", 36);
    ages.put("Lin", 45);
    System.out.println(ages.get("Ada"));
    System.out.println(ages.getOrDefault("Max", -1));
  }
}

Keys are unique

Putting the same key again overwrites the previous value. containsKey andcontainsValue answer membership questions.

Example

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map stock = new HashMap<>();
    stock.put("tea", 10);
    stock.put("tea", 12);   // replaces 10
    System.out.println(stock.get("tea"));           // 12
    System.out.println(stock.containsKey("cake"));  // false
  }
}

Iterate entries

Loop with entrySet() to see both key and value, or keySet() /values() when you only need one side.

Example

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map scores = new HashMap<>();
    scores.put("A", 90);
    scores.put("B", 80);
    for (Map.Entry e : scores.entrySet()) {
      System.out.println(e.getKey() + " -> " + e.getValue());
    }
  }
}

Prefer immutable keys (String, records, …). If a key’s equals /hashCode change after insert, the map can lose the entry.

Counting with merge

Frequency maps are a classic use: increment the count for each word or id. merge keeps the update in one line.

Example

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    String[] words = {"a", "b", "a", "c", "a"};
    Map count = new HashMap<>();
    for (String w : words) {
      count.merge(w, 1, Integer::sum);
    }
    System.out.println(count);   // {a=3, b=1, c=1}
  }
}

Map.of builds a small immutable map. Use new HashMap<>() when you need toput after creation.

Try It Yourself

Exercise: Build a Map<String, Double> of two product prices, print one with get, then print every entry.

Show solution
import java.util.HashMap;
import java.util.Map;

Map prices = new HashMap<>();
prices.put("tea", 2.5);
prices.put("cake", 4.0);
System.out.println(prices.get("tea"));
for (Map.Entry e : prices.entrySet()) {
  System.out.println(e.getKey() + " " + e.getValue());
}

put inserts; entrySet walks each key/value pair once.

Key Takeaways

  • Maps store unique keys mapped to values; put overwrites on the same key.
  • Use get / getOrDefault to read; containsKey to test.
  • Iterate with entrySet() when you need both sides.
  • merge is ideal for counting and aggregating by key.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Chemistry

Atomic number lookup

A map stores a key and a value. Fe maps to 26. Lookup is by symbol, not by scanning a list.

Example

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map<String, Integer> z = new HashMap<>();
    z.put("Fe", 26);
    System.out.println(z.get("Fe"));
  }
}

FAQ: Java Map

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java hashmap examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java hashmap in this Java Java lesson (Java Map).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.