private static boolean hasDuplicates(List<String> list) {
Set<String> uniqueElements = new HashSet<>();
for (String element : list) {
if (uniqueElements.contains(element)) {
return true; // Found a duplicate
} else {
uniqueElements.add(element);
}
}
return false; // No duplicates found
}
private static boolean hasDuplicates(List<String> list) {
Set<String> uniqueElements = new HashSet<>();
for (String element : list) {
if (uniqueElements.contains(element)) {
return true; // Found a duplicate
}
uniqueElements.add(element);
}
return false; // No duplicates found
}
and this one should not trigger:
private static boolean hasDuplicates(List<String> list) {
Set<String> uniqueElements = new HashSet<>();
for (String element : list) {
uniqueElements.add(element);
if (uniqueElements.contains(element)) {
return true; // Found a duplicate
}
}
return false; // No duplicates found
}
Summary
See the example below.
Lint Name
COMMON_REIMPLEMENTATION_ITERABLE_DUPLICATES
Reproducer
and this one should not trigger:
(the contains has to be before the add call)