Created
November 19, 2015 11:01
-
-
Save ghusta/b8cd8ef34973886eb2a9 to your computer and use it in GitHub Desktop.
DuplicatesFinder - Java 5 code
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
package fr.husta.collections; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
/** | |
* Utility for duplicates finding in a list. | |
* <br/> | |
* Utilitaire pour la recherche de doublons dans une liste. | |
* | |
* <p> | |
* See also : <a href="http://stackoverflow.com/questions/8108179/finding-duplicates-in-a-collection">Finding duplicates in a collection</a> | |
* </p> | |
* | |
* @since Java 1.5 | |
*/ | |
public class DuplicatesFinder | |
{ | |
/** | |
* Find duplicates, comparison based on a {@code Comparator}. | |
* | |
* @param list Source list | |
* @param comp Comparator | |
* @param <T> Type of the collection | |
* @return List of duplicates | |
*/ | |
public static <T> List<T> findDuplicates(List<T> list, Comparator<? super T> comp) | |
{ | |
if (list == null || list.size() < 2) | |
{ | |
return Collections.emptyList(); | |
} | |
List<T> listeDoublons = new ArrayList<T>(); | |
for (int i = 0; i < list.size(); i++) | |
{ | |
for (int j = i + 1; j < list.size(); j++) | |
{ | |
T item1 = list.get(i); | |
T item2 = list.get(j); | |
if (i != j && comp.compare(item1, item2) == 0) | |
{ | |
// duplicate ! | |
listeDoublons.add(item2); | |
} | |
} | |
} | |
return listeDoublons; | |
} | |
public static <T> int countDuplicates(List<T> list, Comparator<? super T> comp) | |
{ | |
return findDuplicates(list, comp).size(); | |
} | |
/** | |
* Find duplicates, comparison based on {@code Comparable} interface. | |
* <br/> | |
* Nulls are ignored. | |
* | |
* @param list Source list | |
* @param <T> Type of the collection | |
* @return List of duplicates | |
*/ | |
public static <T extends Comparable<T>> List<T> findDuplicatesOnComparable(List<T> list) | |
{ | |
if (list == null || list.size() < 2) | |
{ | |
return Collections.emptyList(); | |
} | |
List<T> listeDoublons = new ArrayList<T>(); | |
for (int i = 0; i < list.size(); i++) | |
{ | |
for (int j = i + 1; j < list.size(); j++) | |
{ | |
T item1 = list.get(i); | |
T item2 = list.get(j); | |
if (i != j && item1 != null && item1.compareTo(item2) == 0) | |
{ | |
// duplicate ! | |
listeDoublons.add(item2); | |
} | |
} | |
} | |
return listeDoublons; | |
} | |
public static <T extends Comparable<T>> int countDuplicatesOnComparable(List<T> list) | |
{ | |
return findDuplicatesOnComparable(list).size(); | |
} | |
/** | |
* Prefer this method : comparison based on {@code equals()}. | |
* <br/> | |
* Nulls are ignored. | |
* | |
* @param list Source list | |
* @param <T> Type of the collection | |
* @return Set of duplicated elements | |
*/ | |
public static <T> Set<T> findDuplicatedElementsOnEquals(List<T> list) | |
{ | |
if (list == null || list.size() < 2) | |
{ | |
return Collections.emptySet(); | |
} | |
Set<T> setEltsDuplicates = new HashSet<T>(); | |
for (int i = 0; i < list.size(); i++) | |
{ | |
for (int j = i + 1; j < list.size(); j++) | |
{ | |
T item1 = list.get(i); | |
T item2 = list.get(j); | |
if (i != j && item1 != null && item1.equals(item2)) | |
{ | |
// duplicate ! | |
setEltsDuplicates.add(item2); | |
} | |
} | |
} | |
return setEltsDuplicates; | |
} | |
public static <T> int countDuplicatedElementsOnEquals(List<T> list) | |
{ | |
return findDuplicatedElementsOnEquals(list).size(); | |
} | |
/** | |
* Find duplicated elements indices : comparison based on {@code equals()}. | |
* <br/> | |
* Nulls are ignored. | |
* | |
* @param list Source list | |
* @param <T> Type of the collection | |
* @return Set of duplicated indices | |
*/ | |
public static <T> Set<Integer> findDuplicatedIndicesOnEquals(List<T> list) | |
{ | |
if (list == null || list.size() < 2) | |
{ | |
return Collections.emptySet(); | |
} | |
Set<Integer> setIndexDuplicates = new HashSet<Integer>(); | |
for (int i = 0; i < list.size(); i++) | |
{ | |
for (int j = i + 1; j < list.size(); j++) | |
{ | |
T item1 = list.get(i); | |
T item2 = list.get(j); | |
if (i != j && item1 != null && item1.equals(item2)) | |
{ | |
// duplicate ! | |
setIndexDuplicates.add(j); | |
} | |
} | |
} | |
return setIndexDuplicates; | |
} | |
public static <T> int countDuplicatedIndicesOnEquals(List<T> list) | |
{ | |
return findDuplicatedIndicesOnEquals(list).size(); | |
} | |
} |
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
package fr.husta.collections; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
import org.apache.commons.lang.builder.CompareToBuilder; | |
import org.junit.Test; | |
/** | |
* | |
*/ | |
public class DuplicatesFinderTest | |
{ | |
@Test | |
public void testCountDoublonComparable() throws Exception | |
{ | |
List<Integer> liste = Arrays.asList(1, 2, 3, 4, 2, 5, 4); | |
assertFalse(liste.isEmpty()); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.countDuplicatesOnComparable(liste); | |
System.out.println(nbDoublons); | |
assertEquals(2, nbDoublons); | |
} | |
@Test | |
public void testFindDoublonEquals() throws Exception | |
{ | |
List<Integer> liste = Arrays.asList(1, 2, 3, 4, 2, 5, 4); | |
assertFalse(liste.isEmpty()); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.findDuplicatedElementsOnEquals(liste).size(); | |
System.out.println(nbDoublons); | |
assertEquals(2, nbDoublons); | |
} | |
@Test | |
public void testFindDoublonEquals_listeVide() throws Exception | |
{ | |
List<Integer> liste = new ArrayList<Integer>(); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.findDuplicatedElementsOnEquals(liste).size(); | |
System.out.println(nbDoublons); | |
assertEquals(0, nbDoublons); | |
} | |
@Test | |
public void testFindDoublonEquals_listeUn() throws Exception | |
{ | |
List<Integer> liste = new ArrayList<Integer>(); | |
liste.add(10); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.findDuplicatedElementsOnEquals(liste).size(); | |
System.out.println(nbDoublons); | |
assertEquals(0, nbDoublons); | |
} | |
@Test | |
public void testFindDoublonEquals_presenceNull() throws Exception | |
{ | |
List<Integer> liste = Arrays.asList(1, 2, null, null, 4); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.findDuplicatedElementsOnEquals(liste).size(); | |
System.out.println(nbDoublons); | |
assertEquals(0, nbDoublons); | |
} | |
@Test | |
public void testFindDoublonEquals_listeDoublonsMultiplesSeuls() throws Exception | |
{ | |
List<Integer> liste = new ArrayList<Integer>(); | |
liste.add(10); | |
liste.add(10); | |
liste.add(10); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.findDuplicatedElementsOnEquals(liste).size(); | |
System.out.println(nbDoublons); | |
assertEquals(1, nbDoublons); | |
} | |
@Test | |
public void testFindDoublonEquals_listeDoublonsMultiples() throws Exception | |
{ | |
List<Integer> liste = new ArrayList<Integer>(); | |
liste.add(50); | |
liste.add(10); | |
liste.add(60); | |
liste.add(10); | |
liste.add(90); | |
liste.add(10); | |
liste.add(70); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.findDuplicatedIndicesOnEquals(liste).size(); | |
System.out.println(nbDoublons); | |
assertEquals(2, nbDoublons); | |
} | |
@Test | |
public void testFindDoublonEquals_listeDoublonsDebutFin() throws Exception | |
{ | |
List<Integer> liste = new ArrayList<Integer>(); | |
liste.add(10); | |
liste.add(2); | |
liste.add(3); | |
liste.add(4); | |
liste.add(5); | |
liste.add(10); | |
int nbDoublons; | |
nbDoublons = DuplicatesFinder.findDuplicatedIndicesOnEquals(liste).size(); | |
System.out.println(nbDoublons); | |
assertEquals(1, nbDoublons); | |
} | |
@Test | |
public void testCountDoublonWithComparator() throws Exception | |
{ | |
List<Integer> liste = Arrays.asList(1, 2, 3, 4, 2, 5, 4); | |
assertFalse(liste.isEmpty()); | |
int nbDoublons; | |
Comparator<Integer> compInteger = new Comparator<Integer>() | |
{ | |
@Override | |
public int compare(Integer o1, Integer o2) | |
{ | |
return o1.compareTo(o2); | |
} | |
}; | |
nbDoublons = DuplicatesFinder.countDuplicates(liste, compInteger); | |
assertEquals(2, nbDoublons); | |
} | |
@Test | |
public void testCountDoublonWithComparator_presenceNull() throws Exception | |
{ | |
List<Integer> liste = Arrays.asList(1, 2, null, null, 4); | |
assertFalse(liste.isEmpty()); | |
int nbDoublons; | |
Comparator<Integer> compInteger = new Comparator<Integer>() | |
{ | |
@Override | |
public int compare(Integer o1, Integer o2) | |
{ | |
// ne gere pas les null | |
return o1.compareTo(o2); | |
} | |
}; | |
Comparator<Integer> compIntegerNullable = new Comparator<Integer>() | |
{ | |
@Override | |
public int compare(Integer o1, Integer o2) | |
{ | |
return new CompareToBuilder().append(o1, o2).toComparison(); | |
} | |
}; | |
// nbDoublons = DuplicatesFinder.countDuplicates(liste, compInteger); | |
nbDoublons = DuplicatesFinder.countDuplicates(liste, compIntegerNullable); | |
assertEquals(1, nbDoublons); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment