-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashmapExample.java
More file actions
43 lines (31 loc) · 1.34 KB
/
HashmapExample.java
File metadata and controls
43 lines (31 loc) · 1.34 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
import java.util.HashMap;
public class HashmapExample {
public static void main(String[] args){
HashMap<String, String> courseTeacher = new HashMap<String, String>();
courseTeacher.put("History", "Ben");
courseTeacher.put("Mathematics", "Jeanette");
courseTeacher.put("Physics", "Lily");
System.out.println("Courses offered at our Institute");
for(String i: courseTeacher.keySet()){
System.out.println(i);
}
System.out.println("\nTeachers teaching at our Institute");
for(String i: courseTeacher.values()){
System.out.println(i);
}
alternativeHashMapExample();
}
public static void alternativeHashMapExample(){
HashMap<String, String> courseTeacher = new HashMap<String, String>();
courseTeacher.put("History", "Ben");
courseTeacher.put("Mathematics", "Jeanette");
courseTeacher.put("Physics", "Lily");
getEntries(courseTeacher);
}
public static void getEntries(HashMap<String, String> getEntries){
for(HashMap.Entry<String, String> entry: getEntries.entrySet()){
System.out.printf("Course: %s",entry.getKey());
System.out.printf("Teacher: %s \n", entry.getValue());
}
}
}