Java HashMap — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 42 / 50 · தமிழ்

Java HashMap

Look up a value by its key.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

  • Explain the key idea behind HashMap.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

A Map associates keys with values. Each key has at most one mapped value; putting the same key again replaces its mapping. HashMap does not promise a stable iteration order, so do not infer business order from printed map entries.

02 / Step by step

விரிவான விளக்கங்கள்

Java HashMap என்னன்னு தெரியுமா?

HashMap key-value pairs-ல data store பண்றது. Key unique-ஆ இருக்கணும், value duplicate ஆகலாம்.

HashMap use பண்றதுக்கு java.util-ஐ import பண்ணணும்:

HashMap Methods

  • put() — key-value pair add பண்றதுக்கு
  • get() — key-ல value access பண்றதுக்கு
  • remove() — entry remove பண்றதுக்கு
  • containsKey() — key இருக்கான்னு check பண்றதுக்கு
  • size() — entries count
  • keySet() — எல்லா keys-உம் கிடைக்கும்
  • values() — எல்லா values-உம் கிடைக்கும்

HashMap Time Complexity

OperationAverage CaseWorst Case
put(key, value)O(1)O(n) — hash collision
get(key)O(1)O(n) — hash collision
remove(key)O(1)O(n) — hash collision
containsKey(key)O(1)O(n)
containsValue(v)O(n)O(n)

Iterating a HashMap

Java reference fragmentJAVA
HashMap<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 88);
scores.put("Carol", 72);

// entrySet loop
for (Map.Entry<String, Integer> e : scores.entrySet()) {
    System.out.println(e.getKey() + ": " + e.getValue());
}

// forEach (Java 8+)
scores.forEach((name, score) -> System.out.println(name + ": " + score));

// getOrDefault
int val = scores.getOrDefault("Zara", 0);  // 0 if not found

`HashMap` thread-safe-ஆ?

  1. ஆம், always thread-safe
  2. இல்லை — multi-threaded environments-ல் ConcurrentHashMap பயன்படுத்தவும்
  3. Only with synchronized keyword
  4. Java 11-ல் இருந்து thread-safe
Reveal answer / விடை

இல்லை — multi-threaded environments-ல் ConcurrentHashMap பயன்படுத்தவும்

`HashMap` thread-safe இல்லை. Multiple threads simultaneously modify செய்தால் data corruption வரலாம். Thread-safe alternative: `ConcurrentHashMap` அல்லது `Collections.synchronizedMap()`. Interview-ல் இது அடிக்கடி கேட்கப்படும்!

03 / Read and predict

முழுமையான உதாரணம்

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
import java.util.HashMap;
public class Main {
    public static void main(String[] args) throws Exception {
        HashMap<String, Integer> scores = new HashMap<>();
        scores.put("Kavi", 80);
        scores.put("Malar", 90);
        scores.put("Kavi", 85);
        System.out.println(scores.get("Kavi"));
        System.out.println(scores.size());
    }
}
Predict first. Then reveal the expected output / வெளியீடு
85
2

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

Practice challenge

Read an absent key, then try getOrDefault with a fallback value.

Reveal the answer and reasoning / விடை

get returns null for an absent mapping here; getOrDefault("Arun", 0) returns 0.

தவிர்க்க வேண்டிய தவறு: A null return can also mean a key explicitly maps to null. containsKey distinguishes those cases.

பாடத்தின் சுருக்கம்

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.