224k views
4 votes
Write a statement to create a Google Guava Multimap instance which contains student ids as keys and the associated values are student profiles.

User Sammarcow
by
6.5k points

1 Answer

4 votes

Answer: provided in the explanation section

Step-by-step 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.

User Max Beikirch
by
7.2k points