-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashmap.java
More file actions
51 lines (37 loc) · 1.17 KB
/
Hashmap.java
File metadata and controls
51 lines (37 loc) · 1.17 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
43
44
45
46
47
48
49
50
51
package lec27;
import java.util.HashMap;
public class Hashmap {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("Apple", "Bekar");
map.put("orange", "bhaut bekaar");
map.put("mango", "badhiya");
System.out.println(map); // not sorted
for(String string:map.keySet()){ // for each iterator
System.out.println(string);
}
for(String string: map.values()){
System.out.println(string);
}
map.remove("Apple");
for(String string:map.keySet()){ // for each iterator
System.out.println(string);
}
System.out.println(frequencyy("aabbcsaa"));
}
// frequency array
public static HashMap<Character,Integer> frequencyy(String str ){
HashMap<Character,Integer> map = new HashMap<>();
for (int i = 0; i <str.length() ; i++) {
char ch = str.charAt(i);
// int count =1;
if(map.containsKey(ch)){
map.put(ch,map.get(ch)+1);
}
else{
map.put(ch,1);
}
}
return map;
}
}