Skip to content

Instantly share code, notes, and snippets.

@juandiegoh
Last active December 16, 2015 12:39
Show Gist options
  • Save juandiegoh/5436099 to your computer and use it in GitHub Desktop.
Save juandiegoh/5436099 to your computer and use it in GitHub Desktop.
Best way (faster) to get the difference from two collections
public List<A> differenceBetween(List<A> minuend, List<A> subtrahend) {
List<A> difference = Lists.newArrayList();
Set<A> subtrahendSets = Sets.newHashSet(subtrahend);
for (A m1 : minuend) {
if (!subtrahendSets.contains(m1)) {
difference.add(m1);
}
}
return difference;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment