I need to represent a many-many object mapping between two different groups of objects using JSON. For example’s sake, lets say my first set of objects is “students” and the second is “courses”. Each student can have many courses, but each course can also have many students. Is there a standard way of representing such a schema, such that lookups are as efficient as possible?
I know that I could, for example, do
"ELEC1000": ["Bob", "Jessica", "Jeff"],
"MECH1000": ["Aaron", "Bob", "Ben", "Sally"]
This would lead to very fast results when trying to find what students are enrolled in ELEC1000, but would make searching for classes Bob is enrolled in inefficient. I suspect I need to use a hashmap of some sort, but am not entirely sure how to implement this.
Thanks in advance!
If you want efficient lookup both ways, you need to have to duplicate the data (in the form of what a database would call an index).
For example, in addition to the map you already have (course => list of students), also build the opposite map (student => list of courses).
Of course, you need to maintain both maps when there are updates.