1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package xxxxxxxxxxx;
import java.util.Arrays;
import java.util.Vector;
public class TestVector {
/** Creates a new instance of TestVector */
public TestVector() {
}
/** Supprime les doublons d'un Vector
* Deux objets sont considéré comme doublons si et seulement si la méthode equals() retourne true
* Attention, cette méthode nécessite une consistence entre la méthode compareTo() et equals()
* @param vector Vector dont on doit supprimer les doublons
*/
public static void removeDuplicateEntries( Vector vector ) {
synchronized(vector) {
/** Index tout les objets tout en les triants
*/
Indexer indexers[] = new Indexer[ vector.size() ];
for(int i = 0 ; i < vector.size() ; i++ ) {
Object o = vector.get(i);
indexers[i] = new Indexer(i,o);
}
Arrays.sort( indexers );
/** Génere un rapport des doublons
*/
int[] doublons = new int[ vector.size() ];
int doublonIndex = 0;
for(int i = 0 ; i < indexers.length - 1; i++ ) {
if( indexers[i].equals( indexers[i+1] ) ) {
doublons[ doublonIndex ] = indexers[i+1].getIndex();
doublonIndex++;
}
}
/** Tri des doublons
*/
int[] doublonsTries = new int[ doublonIndex ];
System.arraycopy(doublons , 0 , doublonsTries , 0 , doublonIndex );
Arrays.sort(doublonsTries);
/** Supprime les doublons dans le vecteur
*/
for(int i = doublonsTries.length - 1 ; i >= 0 ; i-- ) {
System.out.println("Suppression de l'entrée " + doublonsTries[i] );
vector.removeElementAt( doublonsTries[i] );
}
}
}
/** Classe Indexer permettant de trier les objets entre eux
*/
private static class Indexer<T> implements Comparable<Indexer>
{
private int index = 0;
private T object = null;
public Indexer(int index , T o ) {
this.index = index;
this.object = o;
}
public int getIndex() {
return this.index;
}
public T getObject() {
return this.object;
}
public int compareTo(Indexer other) {
if( this.getObject() == other.getObject() ) return 0;
if( this.getObject() == null ) return -1;
if( other.getObject() == null ) return 1;
if( ! (getObject() instanceof Comparable) || other.getObject() instanceof Comparable) {
int a = System.identityHashCode( getObject() );
int b = System.identityHashCode( other.getObject() );
if( a == b ) return 0;
return a-b;
}
return ((Comparable)getObject()).compareTo( other.getObject() );
}
public boolean equals(Object obj) {
if( obj == null ) return false;
if( ! ( obj instanceof Indexer ) ) return false;
Indexer other = (Indexer)obj;
if( this.getObject() == other.getObject() ) return true;
if( this.getObject() == null ) return false;
if( other.getObject() == null ) return false;
return this.getObject().equals( other.getObject() );
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Vector v = new Vector();
v.add( "Coucou");
v.add( "Bonjour");
v.add( "Comment");
v.add( "Coucou");
v.add( "Azerty");
v.add( "Comment");
removeDuplicateEntries(v);
for(int i = 0 ; i < v.size() ; i++ ) {
System.out.println("==>" + v.get(i) );
}
}
} |
Partager