To remove a repeating element from a Vector collection in Java
1 The Vector.contains () method is used to determine whether the element is included, and if it is not included, it is added to the new set, which is applied to the smaller data.
Private vector getnosameobjectvector (vector vector) {
Vector tempvector = new vector ();
for (int i=0;i<vector.size (); i++) {
Object obj = Vector.get (i);
if (!tempvector.contains (obj) {
Newvector.add (obj);
}
}
return tempvector;
}
2) There is also a simple way, hashset set = new HashSet (vector);
A
Private vector getnosameobjectvector (vector vector) {
Vector tempvector = new vector ();
HashSet set = new HashSet (vector);
AddAll (Collection c); Parameters that can accept set and list types
Tempvector.addall (set);
Retrun Tempvector;
}
b
Private vector getnosameobjectvector (vector vector) {
Vector tempvector = new vector ();
HashSet set = new HashSet (vector);
Tempvector = Settovector (set);
Retrun Tempvector;
}
Private Vector Settovector (set Set) {
Vector vector = new vector ();
Iterator i = From.iterator ();
while (I.hasnext ()) {
Vector.add (I.next ());
}
return vector;
}