Застывший с некоторого времени в этой точке, у меня есть класс (A) с 4 полями, 3 объекта arraylist и 1 строковый arraylist. У меня есть переопределенные равенства и метод hashcode соответственно, я создаю несколько объектов класса A и добавляю к хэш-карте для некоторой логики. Но для случайных объектов с одинаковыми значениями я получаю другое значение хеш-кода, и поэтому, когда я пытаюсь выполнить hashmap.get (объект) случайным образом, любая помощь приветствуется. Ниже приведен класс.Переопределение метода hashcode для класса со списком объектов в виде полей
import java.util.ArrayList;
import java.util.List;
import com.makinglifeeasy4u.util.AddressBean;
import com.makinglifeeasy4u.util.EmailBean;
import com.makinglifeeasy4u.util.PhoneBean;
public class ContactsMainBean {
String contact_id;
String name;
private List<PhoneBean> phonelist = new ArrayList<PhoneBean>();
private List<EmailBean> emaillist = new ArrayList<EmailBean>();
private List<AddressBean> addressbean = new ArrayList<AddressBean>();
private List<String> notesList = new ArrayList<String>();
public String getContact_id() {
return contact_id;
}
public void setContact_id(String contact_id) {
this.contact_id = contact_id;
}
public List<String> getNotesList() {
return notesList;
}
public void setNotesList(List<String> notesList) {
if (this.notesList != null && this.notesList.size() > 0) {
this.notesList.addAll(notesList);
} else {
this.notesList = notesList;
}
// this.notesList = notesList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<PhoneBean> getPhonelist() {
return phonelist;
}
public void setPhonelist(List<PhoneBean> phonelist) {
if (this.phonelist != null && this.phonelist.size() > 0) {
this.phonelist.addAll(phonelist);
} else {
this.phonelist = phonelist;
}
}
public List<EmailBean> getEmaillist() {
return emaillist;
}
public void setEmaillist(List<EmailBean> emaillist) {
if (this.emaillist != null && this.emaillist.size() > 0) {
this.emaillist.addAll(emaillist);
} else {
this.emaillist = emaillist;
}
// this.emaillist = emaillist;
}
public List<AddressBean> getAddressbean() {
return addressbean;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((addressbean == null) ? 0 : addressbean.hashCode());
result = prime * result
+ ((emaillist == null) ? 0 : emaillist.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((notesList == null) ? 0 : notesList.hashCode());
result = prime * result
+ ((phonelist == null) ? 0 : phonelist.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof ContactsMainBean)) {
return false;
}
ContactsMainBean other = (ContactsMainBean) obj;
if (addressbean == null) {
if (other.addressbean != null) {
return false;
}
} else if (!addressbean.equals(other.addressbean)) {
return false;
}
if (emaillist == null) {
if (other.emaillist != null) {
return false;
}
} else if (!emaillist.equals(other.emaillist)) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (notesList == null) {
if (other.notesList != null) {
return false;
}
} else if (!notesList.equals(other.notesList)) {
return false;
}
if (phonelist == null) {
if (other.phonelist != null) {
return false;
}
} else if (!phonelist.equals(other.phonelist)) {
return false;
}
return true;
}
public void setAddressbean(List<AddressBean> addressbean) {
if (this.addressbean != null && this.addressbean.size() > 0) {
this.addressbean.addAll(addressbean);
} else {
this.addressbean = addressbean;
}
// this.addressbean = addressbean;
}
@Override
public String toString() {
// TODO Auto-generated method stub
// return getName()+":"+getPhonelist();
return getName() + ":" + getPhonelist() + ":" + getEmaillist() + ":"
+ getAddressbean() + ":" + getNotesList() + ":"
+ name.hashCode() + ":" + phonelist.hashCode() + ":"
+ emaillist.hashCode() + ":" + notesList.hashCode();
}
}
У меня есть переопределенный метод hashcode и equals в phonebean, emailbean, addressbean.
Добавление кода beanobject:
public class PhoneBean {
String type;
String number;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((number == null) ? 0 : number.hashCode());
//result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PhoneBean other = (PhoneBean) obj;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
// if (type == null) {
// if (other.type != null)
// return false;
// } else if (!type.equals(other.type))
// return false;
return true;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return getNumber();
}
}
электронной боб:
public class EmailBean {
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmailType() {
return emailType;
}
public void setEmailType(String emailType) {
this.emailType = emailType;
}
String email;
String emailType;
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result
+ ((emailType == null) ? 0 : emailType.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EmailBean other = (EmailBean) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (emailType == null) {
if (other.emailType != null)
return false;
} else if (!emailType.equals(other.emailType))
return false;
return true;
}
}
адрес боб:
public class AddressBean {
String poBox;
String street;
String city;
String state;
String postalCode;
String country;
String addresstype;
public String getPoBox() {
return poBox;
}
public void setPoBox(String poBox) {
this.poBox = poBox;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getAddresstype() {
return addresstype;
}
public void setAddresstype(String addresstype) {
this.addresstype = addresstype;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((addresstype == null) ? 0 : addresstype.hashCode());
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((country == null) ? 0 : country.hashCode());
result = prime * result + ((poBox == null) ? 0 : poBox.hashCode());
result = prime * result
+ ((postalCode == null) ? 0 : postalCode.hashCode());
result = prime * result + ((state == null) ? 0 : state.hashCode());
result = prime * result + ((street == null) ? 0 : street.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AddressBean other = (AddressBean) obj;
if (addresstype == null) {
if (other.addresstype != null)
return false;
} else if (!addresstype.equals(other.addresstype))
return false;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
if (poBox == null) {
if (other.poBox != null)
return false;
} else if (!poBox.equals(other.poBox))
return false;
if (postalCode == null) {
if (other.postalCode != null)
return false;
} else if (!postalCode.equals(other.postalCode))
return false;
if (state == null) {
if (other.state != null)
return false;
} else if (!state.equals(other.state))
return false;
if (street == null) {
if (other.street != null)
return false;
} else if (!street.equals(other.street))
return false;
return true;
}
}
Weired вещь, если я SYSOUT ниже код в ToString() класса , я получить результат ниже
return getName() + ":" + getPhonelist() + ":" + getEmaillist() + ":"
+ getAddressbean() + ":" + getNotesList() + ":"
+ name.hashCode() + ":" + phonelist.hashCode() + ":"
+ emaillist.hashCode() + ":" + notesList.hashCode();
Результат:
12-12 00: 25: 20,133: I/System.out (2317): * Наберите Airtel: [321]: []: [ ]: []: 346070743: 50672: 1: 1
12-12 00: 25: 20.133: I/System.out (2317): * Наберите Airtel: [321]: []: []: []: 346070743: 50672: 1: 1
12-12 00: 25: 20.143: I/System.out (2317): * Наберите Airtel: [321]: []: []: []: 346070743: 50672: 1 : 1
12-12 00: 25: 20.143: I/System.out (2317): * Наберите Airtel: [321]: []: []: []: 346070743: 50672: 1: 31
Я предлагаю вам создать короткий, но * полная * Программа демонстрирует проблему. –
Выполняют ли все ваши типы бобов равными + hashcode? Например, если 'PhoneBean' не будет, то будет использоваться хэш-код идентификатора по умолчанию из' Object'. – McDowell
Я думаю, что может быть что-то не так с вашей реализацией 'hashCode' в' phonebean', 'emailbean' или' addressbean'. Можете ли вы их показать? –