Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This test case passes for me:</p> <pre><code>package com.sandbox; import com.google.common.base.Joiner; import com.google.common.collect.LinkedListMultimap; import com.google.common.collect.Multimap; import org.junit.Test; import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; public class SandboxTest { @Test public void testQuestionInput() { List&lt;Person&gt; persons = new ArrayList&lt;Person&gt;(); persons.add(new Person("Foo", "scientist")); persons.add(new Person("Foo", "teacher")); persons.add(new Person("Bar", "student")); String outputString = getOutputString(persons); assertEquals("Foo, scientist, teacher\n" + "Bar, student\n", outputString); } private String getOutputString(List&lt;Person&gt; in) { Multimap&lt;String, String&gt; map = LinkedListMultimap.create(); for (Person person : in) { map.put(person.name, person.occupation); } StringBuilder buf = new StringBuilder(); Joiner joiner = Joiner.on(", "); for (String key : map.keySet()) { buf.append(key).append(", ").append(joiner.join(map.get(key))).append("\n"); } return buf.toString(); } class Person { Person(String name, String occupation) { this.name = name; this.occupation = occupation; } private String name; private String occupation; /* Getters and setters*/ /* toString method */ } } </code></pre> <p>It takes the list of Persons and passes them into a Multimap. <a href="http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html" rel="nofollow">The Multimap comes from the Google guava library.</a> </p> <p>But, you might want to use maurocchi's answer. it's much cleaner than this. </p>
 

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