Skip to content

Instantly share code, notes, and snippets.

@ailabs-software
Created September 6, 2018 03:28
Show Gist options
  • Save ailabs-software/8bcf9daf62488198e9bd0a6df0cfba6a to your computer and use it in GitHub Desktop.
Save ailabs-software/8bcf9daf62488198e9bd0a6df0cfba6a to your computer and use it in GitHub Desktop.
First of each distinct value
class LordeSandwich
{
static List<E> firstOfEach<E, L>(Iterable<E> sourceIterable, ConsumerSupplierFunction<E, L> strategy)
{
Set<L> seenPrimaryKeys = new Set<L>();
List<E> list = new List<E>();
for (E element in sourceIterable)
{
L primaryKey = strategy(element);
if ( !seenPrimaryKeys.contains(primaryKey) ) {
seenPrimaryKeys.add(primaryKey);
list.add(element);
}
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment