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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:06:23  来源:igfitidea点击:

delete duplicates in Vector of data structure

javadata-structuresvectorduplicates

提问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_nbecause 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 Setwhich 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 Setis

在考虑如何删除重复项时,始终首先考虑Set. 根据定义,aSet

A collection that contains no duplicate elements.

不包含重复元素的集合。

Iterate over your array and add them to a Setimplementation. Use a LinkedHashSetif order is important.

迭代您的数组并将它们添加到Set实现中。使用LinkedHashSetif 顺序很重要。

The elements in the Setwill be unique. You can add them back to your a new Listor clear()the old one and add them there.

中的元素Set将是唯一的。您可以将它们添加回新的Listclear()旧的,然后将它们添加到那里。

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

注意:你应该在你的类中实现一个hashCode()andequals(Object)方法objet_poid_n

Note2:If by Vectoryou mean java.util.Vector, please don't use it. Use an ArrayListor LinkedList.

注2:如果是Vector你的意思java.util.Vector,请不要使用它。使用ArrayListLinkedList

回答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:

最简单的解决方案是:

  1. Implements hashcode/equalsin your objet_poid_n
  2. Add all your elements in a HashSet
  1. 实施hashcode/equals在您的objet_poid_n
  2. 将所有元素添加到一个 HashSet

Then your Setwill contain only one instance of each element.

那么您Set将只包含每个元素的一个实例。

Read about hashcode/equalsand HashSetif necessary. It is worth your time.

阅读有关hashcode/equalsHashSet在必要时。值得你花时间。

回答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);
        }
    };