-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathHashStudents.java
More file actions
50 lines (43 loc) · 1.69 KB
/
Copy pathHashStudents.java
File metadata and controls
50 lines (43 loc) · 1.69 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
import java.util.*;
public class HashStudents {
public static void main(String[] args) {
HashMap<String, List<String>> h1 = new LinkedHashMap<>();
HashMap<String, String> h2 = new LinkedHashMap<>();
// Check if HashMap is empty
System.out.println("is studentsMapping empty?: " + h1.isEmpty());
System.out.println("is studentsMapping empty?: " + h2.isEmpty());
// creating list of subjects and adding to hash map
List<String> subjects = Arrays.asList("Python", "Math", "C");
h1.put("A", subjects);
subjects = Arrays.asList("C", "C++");
h1.put("B", subjects);
subjects = Arrays.asList("C++", "Physics", "Chemistry");
h1.put("C", subjects);
// creating hashmap of faculty
h2.put("Python", "111");
h2.put("Math", "222");
h2.put("C", "333");
h2.put("C++", "444");
// displaying all students and subjects
for (Map.Entry m : h1.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
// displaying all subjects and faculty
for (Map.Entry m : h2.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter a student: ");
String s = sc.nextLine();
System.out.println("Faculties are: ");
h1.forEach((k, v) -> {
if (k.equals(s))
v.forEach(w -> {
for (Map.Entry m : h2.entrySet()) {
if (m.getKey().equals(w))
System.out.println(m.getValue());
}
});
});
}
}