-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapExamples.java
More file actions
42 lines (35 loc) · 1.4 KB
/
MapExamples.java
File metadata and controls
42 lines (35 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// MapExamples.java
// This file illustrates how to use maps in Java, including HashMap and TreeMap, and how to store key-value pairs.
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapExamples {
public static void main(String[] args) {
// Creating a HashMap
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("Apple", 1);
hashMap.put("Banana", 2);
hashMap.put("Cherry", 3);
// Displaying the HashMap
System.out.println("HashMap:");
for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Creating a TreeMap
Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Orange", 4);
treeMap.put("Grape", 5);
treeMap.put("Mango", 6);
// Displaying the TreeMap
System.out.println("\nTreeMap:");
for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Accessing a value
int bananaValue = hashMap.get("Banana");
System.out.println("\nValue associated with 'Banana': " + bananaValue);
// Removing a key-value pair
hashMap.remove("Cherry");
System.out.println("\nHashMap after removing 'Cherry': " + hashMap);
}
}