Skip to content

Instantly share code, notes, and snippets.

@bocato
Last active February 11, 2016 12:04
Show Gist options
  • Save bocato/531c5567985658898aab to your computer and use it in GitHub Desktop.
Save bocato/531c5567985658898aab to your computer and use it in GitHub Desktop.
Find Duplicate Objects Generic Java Collection
public <T> List<T> getDuplicate(Collection<T> list) {
final List<T> duplicatedObjects = new ArrayList<T>();
Set<T> set = new HashSet<T>() {
@Override
public boolean add(T e) {
if (contains(e)) {
duplicatedObjects.add(e);
}
return super.add(e);
}
};
for (T t : list) {
set.add(t);
}
return duplicatedObjects;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment