How can I group a list of objects by two attributes in one iteration?
<p>I'm trying to group a large list of objects by two of their attributes. To demonstrate what I mean, consider the following example.</p>

<pre><code>public class Foo {

    private String attributeA;
    private String attributeB;
    private String anotherAttribute;
}
</code></pre>

<p>I want to group a large list of <code>Foo</code> objects by the attributes <code>attributeA</code> and <code>attributeB</code>. Currently I do the following.</p>

<pre><code>List&lt;Foo&gt; foos = getFoos();
Map&lt;Set&lt;String&gt;, List&lt;String&gt;&gt; groupedFoos = Sets.newHashMap();
Set&lt;String&gt; fooGroup;
for(Foo foo : foos) {
    fooGroup = Sets.newHashMap(foo.getAttributeA(), foo.getAttributeB());

    if (!groupedFoos.containsKey(fooGroup)) {
        groupedFoos.put(fooGroup, Lists.newArrayList(foo));
    } else {
        groupedFoos.get(fooGroup).add(foo);
    }
}
</code></pre>

<p>How can I achieve the same result without using a <code>Map</code> like <code>Map&lt;Set&lt;String&gt;, List&lt;String&gt;&gt;</code>? It is important to do this in one iteration. The values of the attributes <code>attributeA</code> and <code>attributeB</code> can be swapped. So using <code>Pair</code> as the key of the <code>Map</code> is also not an option.</p>