Support Online
Skip to main content

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:

  1. Creating the map structure
  2. Adding and updating data
  3. Delete entry
  4. Using helper methods such as get, size, keySet, values
  5. 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:

GenreDescription
HashMapIt is the fastest Map type. It is suitable for general use. (This will be used in this guide.)
LinkedHashMapPreserves the order of insertion. Its performance is slightly lower than HashMap.
ConcurrentMapIt 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

MethodDescriptionExample
get()Returns the value of the keybaskentler.get("Fransa")
size()Returns the number of recordsint sayi = baskentler.size()
keySet()Returns all keysbaskentler.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)

  1. 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.

  1. What happens if I add the same key again?

The new value replaces the old one. Each key must be unique.

  1. Does HashMap work sequentially?

No. Data is stored out of order. If order is required, LinkedHashMap should be preferred.

  1. Can different types be used in Map?

Yes. For example, ID-name matching can be done with the definition Map<Integer, String>.

  1. 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.