Dicionários e Tabelas Hash em Java 17: Guia Detalhado
Dictionaries and Hash Tables in Java
What is a dictionary and why is it needed?
Imagine you have a notebook. In it, you look up a friend's phone number by their name. The name is the key, and the phone number is the value. In Java, for such "key-value" pairs, there is a special tool — HashMap (dictionary or hash table).
Dictionaries are convenient when you need to quickly find something by a unique name (key). For example:
- Country → Greeting in its language
- Person's name → Phone number
- Day of the week → To-do list
- Product name → Price
Unlike lists (ArrayList), where we search by number (index), in a dictionary we search by key. This is very fast!
Basic HashMap Syntax
To use a dictionary, you need to import the java.util.HashMap library.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Create a dictionary where the key is String, the value is String
HashMap<String, String> greetings = new HashMap<>();
// Add pairs (key, value)
greetings.put("Russia", "Hello");
greetings.put("France", "Bonjour");
greetings.put("Italy", "Ciao");
// Get the value by key
String russianGreeting = greetings.get("Russia");
System.out.println("In Russia they say: " + russianGreeting);
// Check if a key exists
if (greetings.containsKey("Italy")) {
System.out.println("Italy is in the dictionary!");
}
// How many entries total
System.out.println("Total countries: " + greetings.size());
// Delete an entry
greetings.remove("France");
System.out.println("After deletion: " + greetings.size() + " countries");
}
}
Program output:
In Russia they say: Hello
Italy is in the dictionary!
Total countries: 3
After deletion: 2 countries
Key and Value Types
Keys and values can be of different types. The main rule: keys must be unique (non-repeating). If you add an existing key, the old value will be replaced with the new one.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Key — String (product name), value — Double (price)
HashMap<String, Double> prices = new HashMap<>();
prices.put("Apples", 89.50);
prices.put("Milk", 75.00);
prices.put("Bread", 45.00);
// Print the price of apples
System.out.println("Price of apples: " + prices.get("Apples") + " rub.");
// Replace the price (key already exists)
prices.put("Apples", 95.00);
System.out.println("New price of apples: " + prices.get("Apples") + " rub.");
}
}
Program output:
Price of apples: 89.5 rub.
New price of apples: 95.0 rub.
How to iterate over all entries in a dictionary?
Often you need to go through all "key-value" pairs. For this, use a for-each loop.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> phoneBook = new HashMap<>();
phoneBook.put("Masha", "8-912-345-67-89");
phoneBook.put("Petya", "8-923-456-78-90");
phoneBook.put("Anya", "8-934-567-89-01");
// Iterate over all entries
for (String name : phoneBook.keySet()) {
String phone = phoneBook.get(name);
System.out.println(name + ": " + phone);
}
}
}
Program output:
Masha: 8-912-345-67-89
Petya: 8-923-456-78-90
Anya: 8-934-567-89-01
Analysis of Typical Problems
Problem 1: Greeting for a traveler
We have a dictionary of countries and greetings. We need to find the greeting for a given country.
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
HashMap<String, String> greetings = new HashMap<>();
greetings.put("England", "Hello");
greetings.put("Spain", "Hola");
greetings.put("Germany", "Guten Tag");
greetings.put("Japan", "Konnichiwa");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter country: ");
String country = scanner.nextLine();
if (greetings.containsKey(country)) {
System.out.println("Greeting: " + greetings.get(country));
} else {
System.out.println("Sorry, this country is not in the dictionary.");
}
scanner.close();
}
}
Problem 2: Phone directory
We look up a phone number by name. If the name is not found, we report it.
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
HashMap<String, String> contacts = new HashMap<>();
contacts.put("Ivan", "8-111-222-33-44");
contacts.put("Olga", "8-555-666-77-88");
contacts.put("Sergey", "8-999-000-11-22");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
String name = scanner.nextLine();
String phone = contacts.get(name);
if (phone != null) {
System.out.println("Phone number: " + phone);
} else {
System.out.println("Contact not found.");
}
scanner.close();
}
}
Problem 3: Deleting a contact
Delete an entry from the digital notebook by name.
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
HashMap<String, String> notebook = new