How to Use Map Structure in Java? (HashMap Guide)
In Java, Map is a powerful data structure that associates data with key and value pairs.
This structure provides fast access to data via a unique key and forms the basis of many Java projects.
🧠 Technical Summary
Main topic: Java Map interface and HashMap usage
Problem it solves: Storing data with unique keys and providing fast access
Steps:
- Creating the map structure
- Adding and updating data
- Delete entry
- Using helper methods such as get, size, keySet, values
- Querying with containsKey, containsValue and getOrDefault
💡 Map structure is ideal for dictionary or JSON-like data storage.
🧩 Map Types
The Map interface is included in the java.util package. The most common applications are:
| Genre | Description |
|---|---|
| HashMap | It is the fastest Map type. It is suitable for general use. (This will be used in this guide.) |
| LinkedHashMap | Preserves the order of insertion. Its performance is slightly lower than HashMap. |
| ConcurrentMap | It is thread-safe, meaning it works safely with multiple threads. |
⚙️ 1. Creating HashMap
Open JShell and create an empty HashMap:
// String türünde anahtar ve değere sahip bir HashMap oluşturur.
Map<String, String> baskentler = new HashMap<>();
Output:
baskentler ==> {}
➕ 2. Adding and Updating Entries
Use the put() method to add a new record:
// "Türkiye" anahtarına "İzmir" değerini ekler.
baskentler.put("Türkiye", "İzmir");
Update the value using the same key:
// Eski değeri (İzmir) "Ankara" ile değiştirir.
baskentler.put("Türkiye", "Ankara");
Let's add another record:
baskentler.put("Almanya", "Berlin");
Output: {Türkiye=Ankara, Almanya=Berlin}
🗑️ 3. Delete Entry
To remove an entry:
// "Almanya" anahtarını ve değerini siler.
baskentler.remove("Almanya");
To clear all records:
// Tüm anahtar-değer çiftlerini siler.
baskentler.clear();
🔍 4. Useful Map Methods
Let's fill in the map again:
baskentler.put("Türkiye", "Ankara");
baskentler.put("Fransa", "Paris");
📖 Basic Methods
| Method | Description | Example |
|---|---|---|
get() | Returns the value of the key | baskentler.get("Fransa") |
size() | Returns the number of records | int sayi = baskentler.size() |
keySet() | Returns all keys | baskentler.keySet() |
values() | Returns all values | baskentler.values() |
🔎 Query Methods
Check for key existence:
baskentler.containsKey("Türkiye"); // true
Check for existence of value:
baskentler.containsValue("İstanbul"); // false
Search with default value:
baskentler.getOrDefault("İspanya", "Tanımlanmadı"); // "Tanımlanmadı"
❓ Frequently Asked Questions (FAQ)
- What is the difference between HashMap and array?
Arrays hold data with an index, while HashMap associates it with a key. In this way, HashMap provides more flexible and faster access.
- What happens if I add the same key again?
The new value replaces the old one. Each key must be unique.
- Does HashMap work sequentially?
No. Data is stored out of order. If order is required, LinkedHashMap should be preferred.
- Can different types be used in Map?
Yes. For example, ID-name matching can be done with the definition Map<Integer, String>.
- Is HashMap thread-safe?
No. If you will use more than one thread at the same time, ConcurrentHashMap should be selected.
🏁 Conclusion
In this guide, you learned about Java's Map interface and its most common implementation, the HashMap structure. You have practically examined storing, updating and querying data with key-value pairs.
💡 You can try your own Map-based Java projects on GenixNode's high-performance servers and run your code instantly.

