Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could:</p> <ol> <li>Use a map that has a list as the value. <code>Map&lt;KeyType, List&lt;ValueType&gt;&gt;</code>.</li> <li>Create a new wrapper class and place instances of this wrapper in the map. <code>Map&lt;KeyType, WrapperType&gt;</code>.</li> <li>Use a tuple like class (saves creating lots of wrappers). <code>Map&lt;KeyType, Tuple&lt;Value1Type, Value2Type&gt;&gt;</code>.</li> <li>Use mulitple maps side-by-side.</li> </ol> <hr> <h2>Examples</h2> <p><strong>1. Map with list as the value</strong></p> <pre><code>// create our map Map&lt;string, List&lt;Person&gt;&gt; peopleByForename = new HashMap&lt;string, List&lt;Person&gt;&gt;(); // populate it List&lt;Person&gt; people = new ArrayList&lt;Person&gt;(); people.add(new Person("Bob Smith")); people.add(new Person("Bob Jones")); peopleByForename.put("Bob", people); // read from it List&lt;Person&gt; bobs = peopleByForename["Bob"]; Person bob1 = bobs[0]; Person bob2 = bobs[1]; </code></pre> <p>The disadvantage with this approach is that the list is not bound to exactly two values.</p> <p><strong>2. Using wrapper class</strong></p> <pre><code>// define our wrapper class Wrapper { public Wrapper(Person person1, Person person2) { this.person1 = person1; this.person2 = person2; } public Person getPerson1 { return this.person1; } public Person getPerson2 { return this.person2; } private Person person1; private Person person2; } // create our map Map&lt;string, Wrapper&gt; peopleByForename = new HashMap&lt;string, Wrapper&gt;(); // populate it Wrapper people = new Wrapper() peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"), new Person("Bob Jones")); // read from it Wrapper bobs = peopleByForename["Bob"]; Person bob1 = bobs.Person1; Person bob2 = bobs.Person2; </code></pre> <p>The disadvantage to this approach is that you have to write a lot of boiler-plate code for all of these very simple container classes.</p> <p><strong>3. Using a tuple</strong></p> <pre><code>// you'll have to write or download a Tuple class in Java, (.NET ships with one) // create our map Map&lt;string, Tuple2&lt;Person, Person&gt; peopleByForename = new HashMap&lt;string, Tuple2&lt;Person, Person&gt;&gt;(); // populate it peopleByForename.put("Bob", new Tuple2(new Person("Bob Smith", new Person("Bob Jones")); // read from it Tuple&lt;Person, Person&gt; bobs = peopleByForename["Bob"]; Person bob1 = bobs.Item1; Person bob2 = bobs.Item2; </code></pre> <p>This is the best solution in my opinion.</p> <p><strong>4. Multiple maps</strong></p> <pre><code>// create our maps Map&lt;string, Person&gt; firstPersonByForename = new HashMap&lt;string, Person&gt;(); Map&lt;string, Person&gt; secondPersonByForename = new HashMap&lt;string, Person&gt;(); // populate them firstPersonByForename.put("Bob", new Person("Bob Smith")); secondPersonByForename.put("Bob", new Person("Bob Jones")); // read from them Person bob1 = firstPersonByForename["Bob"]; Person bob2 = secondPersonByForename["Bob"]; </code></pre> <p>The disadvantage of this solution is that it's not obvious that the two maps are related, a programmatic error could see the two maps get out of sync.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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