|
| 1 | +What is a Dictionary in Java? |
| 2 | + |
| 3 | +In Java, there is no direct dictionary data type like in Python. |
| 4 | +However, a similar structure can be created using the `HashMap` class from the `java.util` package. |
| 5 | + |
| 6 | +- A `HashMap` stores key-value pairs. |
| 7 | +- **Key**: The unique identifier. |
| 8 | +- **Value**: The data associated with the key. |
| 9 | + |
| 10 | +Example: |
| 11 | + |
| 12 | +HashMap<String, Integer> studentAges = new HashMap<>(); |
| 13 | +studentAges.put("Alice", 20); // Key: "Alice", Value: 20 |
| 14 | +studentAges.put("Bob", 22); // Key: "Bob", Value: 22 |
| 15 | +studentAges.put("Charlie", 19); // Key: "Charlie", Value: 19 |
| 16 | + |
| 17 | + |
| 18 | +How to Iterate Over a Dictionary in Java |
| 19 | +---------------------------------------- |
| 20 | + |
| 21 | +There are three main ways to iterate over a `HashMap`: |
| 22 | + |
| 23 | + |
| 24 | +1. Iterate Over Keys |
| 25 | +-------------------- |
| 26 | +You can retrieve all the keys using the `keySet()` method and loop through them. |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +public class Main { |
| 30 | + public static void main(String[] args) { |
| 31 | + HashMap<String, Integer> studentAges = new HashMap<>(); |
| 32 | + studentAges.put("Alice", 20); |
| 33 | + studentAges.put("Bob", 22); |
| 34 | + studentAges.put("Charlie", 19); |
| 35 | + |
| 36 | + System.out.println("Keys:"); |
| 37 | + for (String key : studentAges.keySet()) { |
| 38 | + System.out.println(key); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +Output: |
| 44 | +Keys: |
| 45 | +Alice |
| 46 | +Bob |
| 47 | +Charlie |
| 48 | + |
| 49 | + |
| 50 | +2. Iterate Over Values |
| 51 | +---------------------- |
| 52 | +You can retrieve all the values using the `values()` method and loop through them. |
| 53 | + |
| 54 | +import java.util.HashMap; |
| 55 | +public class Main { |
| 56 | + public static void main(String[] args) { |
| 57 | + HashMap<String, Integer> studentAges = new HashMap<>(); |
| 58 | + studentAges.put("Alice", 20); |
| 59 | + studentAges.put("Bob", 22); |
| 60 | + studentAges.put("Charlie", 19); |
| 61 | + |
| 62 | + System.out.println("Values:"); |
| 63 | + for (Integer value : studentAges.values()) { |
| 64 | + System.out.println(value); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +Output: |
| 70 | +Values: |
| 71 | +20 |
| 72 | +22 |
| 73 | +19 |
| 74 | + |
| 75 | + |
| 76 | +3. Iterate Over Key-Value Pairs |
| 77 | +------------------------------- |
| 78 | +You can retrieve both keys and values together using the `entrySet()` method. |
| 79 | + |
| 80 | +import java.util.HashMap; |
| 81 | +import java.util.Map; |
| 82 | +public class Main { |
| 83 | + public static void main(String[] args) { |
| 84 | + HashMap<String, Integer> studentAges = new HashMap<>(); |
| 85 | + studentAges.put("Alice", 20); |
| 86 | + studentAges.put("Bob", 22); |
| 87 | + studentAges.put("Charlie", 19); |
| 88 | + |
| 89 | + System.out.println("Key-Value Pairs:"); |
| 90 | + for (Map.Entry<String, Integer> entry : studentAges.entrySet()) { |
| 91 | + System.out.println(entry.getKey() + ": " + entry.getValue()); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +Output: |
| 97 | +Key-Value Pairs: |
| 98 | +Alice: 20 |
| 99 | +Bob: 22 |
| 100 | +Charlie: 19 |
| 101 | + |
| 102 | + |
| 103 | +Step-by-Step Explanation of the Third Example |
| 104 | +--------------------------------------------- |
| 105 | +- `entrySet()` → Returns a set of all key-value pairs in the HashMap. |
| 106 | +- `Map.Entry` → Represents a single key-value pair. |
| 107 | +- `for (Map.Entry<String, Integer> entry : studentAges.entrySet())` → Iterates through each pair. |
| 108 | + |
| 109 | +Inside the loop: |
| 110 | +- `entry.getKey()` → Access the key. |
| 111 | +- `entry.getValue()` → Access the value. |
| 112 | + |
| 113 | + |
| 114 | +Key Methods for Iterating a HashMap |
| 115 | +----------------------------------- |
| 116 | +Method Purpose |
| 117 | +----------- ------------------------------------------------- |
| 118 | +keySet() Returns all the keys in the map. |
| 119 | +values() Returns all the values in the map. |
| 120 | +entrySet() Returns all key-value pairs in the map. |
0 commit comments