-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapInterface.java
More file actions
53 lines (43 loc) · 1.5 KB
/
Copy pathMapInterface.java
File metadata and controls
53 lines (43 loc) · 1.5 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
52
53
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class MapInterface {
public static void main(String[] args) {
Set<Course> courseSet1 = new HashSet<>();
courseSet1.add(new Course("Java"));
courseSet1.add(new Course("DBMS"));
Set<Course> courseSet2 = new HashSet<>();
courseSet2.add(new Course("PHP"));
courseSet2.add(new Course("HTML"));
courseSet2.add(new Course("CSS"));
Map<Integer, Set<Course>> studentCourses = new HashMap<>();
studentCourses.put(1001, courseSet1);
studentCourses.put(1002, courseSet2);
// Retrieving the set of Courses by studentID using get() method
Set<Course> courseSet = studentCourses.get(1001);
System.out.println("Retrieving the set of Courses by studentID: ");
System.out.println(courseSet);
// Iterating over the set of keys using for-each loop
Set<Integer> setOfKeys = studentCourses.keySet();
System.out.println("Iterating over the set of keys using for-each loop: ");
for (Integer i : setOfKeys) {
System.out.println(studentCourses.get(i));
}
// Iterating over the collection using values() method
System.out.println("Iterating over the collection using values() method: ");
for (Set<Course> courses : studentCourses.values()) {
System.out.println(courses);
}
}
}
class Course {
String courseName;
public Course(String courseName) {
this.courseName = courseName;
}
@Override
public String toString() {
return courseName;
}
}