Java 删除数据结构向量中的重复项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18678919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
delete duplicates in Vector of data structure
提问by Ghassen Bellagha
I have a Vector
, each cell is containing a Data Structure
:
我有一个Vector
,每个单元格都包含一个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 ..
问题是 Vector 可能有重复项,我需要创建一个能够删除重复项的函数或过程。
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 ?
有任何想法吗 ?
采纳答案by Cedias
First you should implement an equals()
method in your objet_poid_n
because the default behavior compares memory addresses.
首先你应该equals()
在你的中实现一个方法,objet_poid_n
因为默认行为比较内存地址。
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.
然后你可以在你的向量中插入一个新对象之前使用方法Vector.contains()或者使用 a Set
,根据定义,它是一个没有重复的集合。
回答by Sotirios Delimanolis
When thinking about how to remove duplicates, always first consider a Set
. By definition, a Set
is
在考虑如何删除重复项时,始终首先考虑Set
. 根据定义,aSet
是
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.
迭代您的数组并将它们添加到Set
实现中。使用LinkedHashSet
if 顺序很重要。
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.
中的元素Set
将是唯一的。您可以将它们添加回新的List
或clear()
旧的,然后将它们添加到那里。
Note:You should implement a hashCode()
and equals(Object)
method in your objet_poid_n
class.
注意:你应该在你的类中实现一个hashCode()
andequals(Object)
方法objet_poid_n
。
Note2:If by Vector
you mean java.util.Vector
, please don't use it. Use an ArrayList
or LinkedList
.
注2:如果是Vector
你的意思java.util.Vector
,请不要使用它。使用ArrayList
或LinkedList
。
回答by Marcin Szymczak
Set<String> set = new HashSet<String>();
set.addAll(list);
list.clear();
list.addAll(set);
回答by Jean Logeart
Removing elements in your arrays will change the size and you will try to access indices that no longer exist.
删除数组中的元素将更改大小,您将尝试访问不再存在的索引。
The simplest solution is:
最简单的解决方案是:
- Implements
hashcode
/equals
in yourobjet_poid_n
- Add all your elements in a
HashSet
- 实施
hashcode
/equals
在您的objet_poid_n
- 将所有元素添加到一个
HashSet
Then your Set
will contain only one instance of each element.
那么您Set
将只包含每个元素的一个实例。
Read about hashcode
/equals
and HashSet
if necessary. It is worth your time.
阅读有关hashcode
/equals
并HashSet
在必要时。值得你花时间。
回答by Cedias
Use below method to remove dulplicate elements from vector
使用以下方法从向量中删除重复元素
public static Vector removeDuplicateResults(Vector resultsVector) {
公共静态向量 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;
}
回答by Sepehr Gouran
You could override the add or addAll methods in my case I used data as a local variable:
在我使用数据作为局部变量的情况下,您可以覆盖 add 或 addAll 方法:
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);
}
};