Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Guava has <a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/collect/Maps.html#uniqueIndex%28java.lang.Iterable,%20com.google.common.base.Function%29">Maps.uniqueIndex(Iterable values, Function keyFunction)</a> and <a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/collect/Multimaps.html#index%28java.lang.Iterable,%20com.google.common.base.Function%29">Multimaps.index(Iterable values, Function keyFunction)</a>, but they don't transform the values. There are <a href="http://code.google.com/p/guava-libraries/issues/detail?id=679">some</a> <a href="http://code.google.com/p/guava-libraries/issues/detail?id=56">requests</a> to add utility methods that do what you want, but for now, you'll have to roll it yourself using Multimaps.index() and Multimaps.transformValues():</p> <pre><code>static class Person { private final Integer age; private final String name; public Person(Integer age, String name) { this.age = age; this.name = name; } public Integer getAge() { return age; } public String getName() { return name; } } private enum GetAgeFunction implements Function&lt;Person, Integer&gt; { INSTANCE; @Override public Integer apply(Person person) { return person.getAge(); } } private enum GetNameFunction implements Function&lt;Person, String&gt; { INSTANCE; @Override public String apply(Person person) { return person.getName(); } } public void example() { List&lt;Person&gt; persons = ImmutableList.of( new Person(100, "Alice"), new Person(200, "Bob"), new Person(100, "Charles"), new Person(300, "Dave") ); ListMultimap&lt;Integer, String&gt; ageToNames = getAgeToNamesMultimap(persons); System.out.println(ageToNames); // prints {100=[Alice, Charles], 200=[Bob], 300=[Dave]} } private ListMultimap&lt;Integer, String&gt; getAgeToNamesMultimap(List&lt;Person&gt; persons) { ImmutableListMultimap&lt;Integer, Person&gt; ageToPersons = Multimaps.index(persons, GetAgeFunction.INSTANCE); ListMultimap&lt;Integer, String&gt; ageToNames = Multimaps.transformValues(ageToPersons, GetNameFunction.INSTANCE); // Multimaps.transformValues() returns a *lazily* transformed view of "ageToPersons" // If we want to iterate multiple times over it, it's better to create a copy return ImmutableListMultimap.copyOf(ageToNames); } </code></pre> <hr> <p>A re-usable utility method could be:</p> <pre><code>public static &lt;E, K, V&gt; ImmutableListMultimap&lt;K, V&gt; keyToValuesMultimap(Iterable&lt;E&gt; elements, Function&lt;E, K&gt; keyFunction, Function&lt;E, V&gt; valueFunction) { ImmutableListMultimap&lt;K, E&gt; keysToElements = Multimaps.index(elements, keyFunction); ListMultimap&lt;K, V&gt; keysToValuesLazy = Multimaps.transformValues(keysToElements, valueFunction); return ImmutableListMultimap.copyOf(keysToValuesLazy); } </code></pre> <p>I guess we could improve the generics in the signature by using <code>Function&lt;? extends E, K&gt;</code> or something, but I don't have the time to delve further...</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