Last active
February 11, 2016 12:04
-
-
Save bocato/531c5567985658898aab to your computer and use it in GitHub Desktop.
Find Duplicate Objects Generic Java Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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