如何在java中找到arraylist的重复对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21782975/
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
How to find a duplicate object of arraylist in java
提问by user3094329
Hi I want to find a duplicate object of array list of one class type. I tried using hash set but is not working. Can somebody please help.
嗨,我想找到一个类类型的数组列表的重复对象。我尝试使用哈希集但不起作用。有人可以帮忙吗。
package stream;
import java.util.*;
public class Chumma {
public static void main(String[] args) {
Dummy d = new Dummy();
Dummy d1 = new Dummy();
Dummy d2 = new Dummy();
d.setAge(14);
d.setName("XXX");
d1.setAge(15);
d1.setName("YYY");
d2.setAge(14);
d2.setName("XXX");
List<Dummy> list = new ArrayList<Dummy>();
list.add(d);
list.add(d1);
list.add(d2);
Set<Dummy> uniqueSet = new HashSet<Dummy>(list);
Set uniqueEntries = new HashSet<Dummy>();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (!uniqueEntries.add(element)) // if current element is a duplicate,
// iter.remove();
System.out.println(iter.toString());
}
}
}
回答by Teo
You can use
您可以使用
Collections.frequency(collection, object);
So if frequency
method return a number > 1
it means that you have more same object...
因此,如果frequency
方法返回一个数字,> 1
则意味着您有更多相同的对象...
Collections
is get by java.util.Collections
Collections
过得去 java.util.Collections
and that metod Returns the number of elements in the specified collection equal to the specified
并且该方法返回指定集合中等于指定的元素数
frequency(c, o)
频率(c, o)
回答by Jonathan Kim
Default implementation of equals() method provided by java.lang.Object compares memory location and only return true if two reference variable are pointing to same memory location. equals() method used to avoid duplicates on HashSet.
java.lang.Object 提供的 equals() 方法的默认实现比较内存位置,只有当两个引用变量指向相同的内存位置时才返回 true。equals() 方法用于避免 HashSet 上的重复。
The problem is that you didn't override the equals() and hash() methods.
问题是您没有覆盖 equals() 和 hash() 方法。
A common source of bugs is the failure to override the hashCode method. You must override hashCode in every class that overrides equals. Failure to do so will result in a violation of the general contract for Object.hashCode, which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable. [Effective Java]
错误的一个常见来源是无法覆盖 hashCode 方法。您必须在每个覆盖 equals 的类中覆盖 hashCode。不这样做将导致违反 Object.hashCode 的一般契约,这将阻止您的类与所有基于哈希的集合(包括 HashMap、HashSet 和 Hashtable)一起正常运行。[有效Java]
import java.util.*;
public class HashSetTest {
public static class Dummy {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Dummy)) return false;
Dummy dummy = (Dummy) o;
if (age != dummy.age) return false;
if (name != null ? !name.equals(dummy.name) : dummy.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = age;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
@Override
public String toString() {
return String.format("age:%d, name:%s", age, name);
}
}
public static void main(String[] args) {
Dummy dummy01 = new Dummy();
dummy01.setAge(14);
dummy01.setName("XXXX");
Dummy dummy02 = new Dummy();
dummy02.setAge(15);
dummy02.setName("YYYY");
Dummy dummy03 = new Dummy();
dummy03.setAge(14);
dummy03.setName("XXXX");
List<Dummy> dummies = new ArrayList<Dummy>();
dummies.add(dummy01);
dummies.add(dummy02);
dummies.add(dummy03);
Set<Dummy> uniqueDummies = new HashSet<Dummy>();
for (Dummy dummy : dummies) {
uniqueDummies.add(dummy);
}
System.out.println(uniqueDummies);
}
}
回答by Amar
You Can Visit this Link
您可以访问此链接
Sample Code of this link
此链接的示例代码
//ArrayList with duplicates String
List<String> duplicateList = (List<String>) Arrays.asList("Android" , "Android", "iOS", "Windows mobile");
System.out.println("size of Arraylist with duplicates: " + duplicateList.size()); //should print 4 becaues of duplicates Android
System.out.println(duplicateList);
//Converting ArrayList to HashSet to remove duplicates
HashSet<String> listToSet = new HashSet<String>(duplicateList);
//Creating Arraylist without duplicate values
List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
System.out.println("size of ArrayList without duplicates: " + listToSet.size()); //should print 3 becaues of duplicates Android removed
System.out.println(listWithoutDuplicates);
Output:
size of Arraylist with duplicates: 4
[Android, Android, iOS, Windows mobile]
size of ArrayList without duplicates: 3
[Android, Windows mobile, iOS]