-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashMapPrac.java
More file actions
27 lines (26 loc) · 1.01 KB
/
HashMapPrac.java
File metadata and controls
27 lines (26 loc) · 1.01 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
import java.util.HashMap;
import java.util.Map;
public class HashMapPrac {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.put("Charlie", 92);
scores.put(null, 100);
scores.put("Bob", 88);
scores.put("Bob", 8);
scores.put("Bob", 100);
System.out.println("Score of Alice: " + scores.get("Alice"));
System.out.println("Contains Bob? " + scores.containsKey("Bob"));
System.out.println("Contains 92? " + scores.containsValue(92));
for (Map.Entry<String, Integer> e : scores.entrySet()) {
System.out.println(e.getKey() + " -> " + e.getValue());
}
String[] words = {"apple", "banana", "apple", "kiwi", "banana", "apple"};
Map<String, Integer> freq = new HashMap<>();
for (String w : words) {
freq.merge(w, 1, Integer::sum);
}
System.out.println("Frequencies: " + freq);
}
}