Skip to content

Instantly share code, notes, and snippets.

@djalexd
Created October 26, 2015 16:15
Show Gist options
  • Select an option

  • Save djalexd/d960f7d5f8a93747e3dc to your computer and use it in GitHub Desktop.

Select an option

Save djalexd/d960f7d5f8a93747e3dc to your computer and use it in GitHub Desktop.
class T<V> {
private String key;
private Set<V> items;
}
You have a Collection<T> and are required to return a Collection<T> where T objects
that have same 'key' will contains grouped 'items'.
Example:
T t1 = new T("key1", ImmutableSet.of(v1, v2, v3));
T t2 = new T("key2", ImmutableSet.of(v3, v4));
T t3 = new T("key1", ImmutableSet.of(v1, v5, v6));
Collection<T> items = ImmutableSet.of(t1, t2, t3);
Collection<T> items2 = grouByKey(items);
// items2 has this structure:
[
new T("key1", ImmutableSet.of(v1, v2, v3, v5, v6)),
new T("key2", ImmutableSet.of(v3, v4))
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment