0

I have a Vector, each cell is containing a Data Structure :

public class objet_poid_n {
   public  int Num;
    public double Poid;
}

The problem is that the Vector may have duplications and i need to create a function or procedure able to delete duplicates .. I tried this one and it couldn't help ..

 public static void removeDuplicates(Vector v)
{ 
    for(int i=0;i<v.size();i++){
            for(int j=0;j<v.size();j++)
                {
                    if(i!=j)
                        {
                            if(v.elementAt(i).equals(v.elementAt(j)))
                                {
                                    v.removeElementAt(j);
                                }
                        }
                }
                                   }
} 

Any Ideas ?

Ghassen Bellagha
  • 249
  • 3
  • 5
  • 16

6 Answers6

1

When thinking about how to remove duplicates, always first consider a Set. By definition, a Set is

A collection that contains no duplicate elements.

Iterate over your array and add them to a Set implementation. Use a LinkedHashSet if order is important.

The elements in the Set will be unique. You can add them back to your a new List or clear() the old one and add them there.

Note: You should implement a hashCode() and equals(Object) method in your objet_poid_n class.

Note2: If by Vector you mean java.util.Vector, please don't use it. Use an ArrayList or LinkedList.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1
Set<String> set = new HashSet<String>();
set.addAll(list);
list.clear();
list.addAll(set);
Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
1

First you should implement an equals() method in your objet_poid_n because the default behavior compares memory addresses.

Then you could either use the method Vector.contains() before inserting a new object in your vector or use a Set which is by definition a collection without duplicates.

Community
  • 1
  • 1
Cedias
  • 904
  • 10
  • 19
  • `public boolean equals(Object obj) { return (this == obj); }` Then you mean i use a loop to compare ìf(Vector.contains(newitem) == true) Vector.add(newitem)` ?? – Ghassen Bellagha Sep 07 '13 at 23:50
  • `public boolean equals(objet_poid_n obj) { if((obj.Num == this.Num) && (obj.Poid == this.Poid)) return true; else return (false); }` – Ghassen Bellagha Sep 07 '13 at 23:57
  • have a look here: http://stackoverflow.com/questions/185937/overriding-the-java-equals-method-quirk – Cedias Sep 08 '13 at 00:04
  • Logically what you say and what you have mentioned is logical and it should do remove duplicates ! and i think that the problem is in the code later . i will mention your answer as a correct answer as a solution for resolving the Duplications of a Data structure Vector and i will check my code maybe the error is not there :) – Ghassen Bellagha Sep 08 '13 at 00:08
0

Removing elements in your arrays will change the size and you will try to access indices that no longer exist.

The simplest solution is:

  1. Implements hashcode/equals in your objet_poid_n
  2. Add all your elements in a HashSet

Then your Set will contain only one instance of each element.

Read about hashcode/equals and HashSet if necessary. It is worth your time.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

Use below method to remove dulplicate elements from vector

public static Vector removeDuplicateResults(Vector resultsVector) {

    for(int i=0;i<resultsVector.size();i++){        
        for(int j=0;j<resultsVector.size();j++){            
                if(i!=j){                                            
                    Object resultsVectorObject1 = resultsVector.elementAt(i);
                    Object resultsVectorObject2 = resultsVector.elementAt(j);
                    Object[] resultsObjectArray1 = (Object[]) resultsVectorObject1;
                    Object[] resultsObjectArray2 = (Object[]) resultsVectorObject2;
                    if(Arrays.equals(resultsObjectArray1, resultsObjectArray2))
                    {
                        resultsVector.removeElementAt(j);
                    }
                }
        }
    }
    return resultsVector;
} 
0

You could override the add or addAll methods in my case I used data as a local variable:

data = new Vector<String>() {
        @Override
        public synchronized boolean addAll(Collection<? extends String> arg0) {
            // TODO Auto-generated method stub
            Vector<String> v = (Vector<String>) arg0;
            for (String string : v) {
                if (contains(string)) {
                    return false;
                }
            }
            return super.addAll(v);
        }
    };