title: Switch direction of association between objects
body: <p>Say I have a class <code>foo</code>:</p>
  
  <pre><code>class foo {
      String someString;
      List&lt;String&gt; someList;
  }
  </code></pre>
  
  <p>If I have a list of <code>foo</code>'s, is there a way for me to create a new list/array/whatever of <code>foo</code>'s with essentially the <code>someString</code> and <code>someList</code> values remapped? For example:</p>
  
  <pre><code>arr1: [
      foo {
          someString: 'test1',
          someList: ['a', 'b']
      },
      foo {
          someString: 'test2',
          someList: ['b', 'c']
      }
  ]
  </code></pre>
  
  <p>Would become</p>
  
  <pre><code>arr2: [
      foo {
          someString: 'a',
          someList: ['test1']
      },
      foo {
          someString: 'b',
          someList: ['test1', 'test2']
      },
      foo {
          someString: 'c',
          someList: ['test2']
      },
  ]
  </code></pre>
  
  <p>Right now, I have a nested loop that iterates over each element of <code>someList</code> for each foo and compiles them into a map where the key is the <code>someList</code> value and the value is a set of the <code>someString</code> values that have come from foos where the <code>someList</code> value was present. Then do a map of the entry set to convert it to new foos with the key and value passed as constructor params.</p>
  
  <pre><code>Map&lt;String, Set&lt;String&gt;&gt; myMap;
  
  for(foo f : arr1) {
      for(String s : f.someList) {
          Set&lt;String&gt; mySet = myMap.get(s);
          if(mySet == null) {
              myMap.put(s, new HashSet&lt;String&gt;(Arrays.asList(f.someString)));
          } else {
              mySet.add(f.someString);
          }
      }
  }
  
  List&lt;String&gt; myNewList = myMap.entrySet()
                                .stream()
                                .map(e -&gt; 
                                      new foo(e.key, new ArrayList&lt;String&gt;(e.value)))
                                .collect(Collectors.toList());
  </code></pre>
  
  <p>It seems to work fine, but it's not the prettiest in the world, so I was wondering if there's another approach to doing this.</p>