Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd advise using <code>Map&lt;Lecturer, List&lt;Course&gt;&gt;</code>, as someone replied in the previous question, which means, "an association (<code>Map</code>) between different lecturers (<code>Lecturer</code>) to a list of the courses they teach (<code>List&lt;Course&gt;</code>), instead, which you would instantiate as <code>new HashMap&lt;Lecturer, List&lt;Course&gt;&gt;</code>. Otherwise, storing a list for each, you'll be duplicating functionality.</p> <p>Once you declared the <code>Course</code>s <code>c1, c2, ..., cn</code> a <code>Lecturer</code> <code>l</code> teaches, you associate them in the <code>Map</code> <code>m</code> as <code>m.put(l, c)</code> where <code>c</code> is a <code>List</code> of the courses, declared as <code>new LinkedList&lt;Course&gt;()</code>, and added as <code>c.add(c1); c.add(c2); ... c.add(cn);</code>.</p> <p>If I explain myself clearly.</p> <p>You can read <a href="http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html" rel="nofollow noreferrer">http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html</a> and <a href="http://java.sun.com/docs/books/tutorial/collections/implementations/map.html" rel="nofollow noreferrer">http://java.sun.com/docs/books/tutorial/collections/implementations/map.html</a> for more help on using <code>Map</code>s.</p> <p>To obtain the reverse association, you can easily use the following code:</p> <pre><code>Collection&lt;Course&gt; courses = m.values(); Map&lt;Course, List&lt;Lecturer&gt;&gt; reverseAssociation = new HashMap&lt;Course, List&lt;Lecturer&gt;&gt;; for (Course course : courses) { List&lt;Lecturer&gt; lecturersTeachingCourse = new LinkedList&lt;Lecturer&gt;(); for (Lecturer lecturer : m.keySet()) { if (lecturer.teaches(course)) { lecturersTeachingCourse.add(lecturer); } } coursesTaught.put(course, lecturersTeachingCourse); } </code></pre> <p>Which, as long as <code>Lecturer.teaches(Course)</code> queries whether the lecturer teaches the passed course, sets <code>reverseAssociation</code> as the courses-lecturers association. (Naturally, you should encapsulate that code as a method in Lecturers).</p> <p>Good luck JavaNoob! We were all there once!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload