Answer: provided in the explanation section
Explanation:
Note: take note for indentation so as to prevent error.
So we import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.Map;
class Main {
public static void main(String[] args) {
// key as studentId and List of Profile as value.
Multimap<String,Profile> multimap = ArrayListMultimap.create();
multimap.put("id1", new ExamProfile("examStudentId1"));
multimap.put("id1", new ELearningProfile("userId1"));
multimap.put("id2", new ExamProfile("examStudentId2"));
multimap.put("id2", new ELearningProfile("userId2"));
// print the data now.
for (Map.Entry<String, Collection<Profile>> entry : multimap.entrySet()) {
String key = entry.getKey();
Collection<String> value = multimap.get(key);
System.out.println(key + ":" + value);
}
}
}
// assuming String as the studentId.
// assuming Profile as the interface. And Profile can multiple implementations that would be
// specific to student.
interface Profile {
}
class ExamProfile implements Profile {
private String examStudentId;
public ExamProfile(String examStudentId) {
this.examStudentId = examStudentId;
}
}
class ELearningProfile implements Profile {
private String userId;
public ELearningProfile(String userId) {
this.userId = userId;
}
}
This code is able to iterate through all entries in the Google Guava multimap and display all the students name in the associated students profile.