如何抓在下面的代碼對象數組的唯一列表:抓取對象數組的唯一列表
import java.util.*;
class t1 {
public static void main(String[] args) {
Object[] o1 = new Object[] { null, "[email protected]", "ENG", "775778435", 1};
Object[] o2 = new Object[] { null, "[email protected]", "ENG", "775778435", 1};
List<Object[]> result = new ArrayList<Object[]>();
result.add(o1);
result.add(o2);
// The above result list is coming from some DB & I cannot change the structure of above script.
// Now, I need to remove the duplicates from this result list meaning I need to find duplicates from the objects within the list.
// I tried below code but it still prints duplicates. Any help???
Set<User> setResult = new HashSet<User>();
User userInfo = null;
for (Object[] userData : result) {
userInfo = new User((String)userData[0], (String)userData[1], (String)userData[2], (String)userData[3], (Integer) userData[4]);
setResult.add(userInfo);
}
Iterator it = setResult.iterator();
while (it.hasNext()) {
Object o = it.next();
User u = (User) o;
System.out.println("non-duplicate = " + u.getEmail());
}
// Expected result: non-duplicate = [email protected]
// Actual result: non-duplicate = [email protected] getting printed twice i.e. duplicate not getting removed!
}
}
class User {
public String firstName;
public String email;
public String language;
public String productCode;
public int status;
public User() {
}
public User(String fName, String userId, String lang, String productCode, int status) {
this.firstName = fName;
this.email = userId;
this.language = lang;
this.productCode = productCode;
this.status = status;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Override
public int hashCode() {
int fNameHash = 0;
int lNameHash = 0;
int emailHash = 0;
int langHash = 0;
int productCodeHash = 0;
if (this.firstName != null) {
fNameHash = this.firstName.hashCode();
}
if (this.email != null) {
emailHash = this.email.hashCode();
}
if (this.language != null) {
langHash = this.language.hashCode();
}
if (this.productCode != null) {
productCodeHash = this.productCode.hashCode();
}
return (fNameHash + lNameHash + emailHash + langHash + productCodeHash + this.status);
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof User) {
User temp = (User) obj;
if (this.firstName != null && temp.firstName != null && this.firstName.equalsIgnoreCase(temp.firstName)
&& this.email != null && temp.email != null && this.email.equalsIgnoreCase(temp.email)
&& this.language != null && temp.language != null && this.language.equalsIgnoreCase(temp.language)
&& this.productCode != null && temp.productCode != null && this.productCode.equalsIgnoreCase(temp.productCode)
&& this.status == temp.status) {
return true;
}
}
return false;
}
}
我預期的結果是打印[email protected]只有一次,但其得到印刷兩次!
任何人都可以告訴我如何更正此代碼嗎?
謝謝!
名稱可以爲空,這就是要求。我如何解決它? – Mike 2012-08-19 23:34:39
如果我在equals方法中刪除了fName的空檢查,它會給出空指針異常....那麼我該怎麼做呢? – Mike 2012-08-19 23:40:47