我在数组列表中有很多对象。有一些重复。我想从中删除重复项。 我尝试使用下面的代码。
下面是我的PhoneContacts模型类
public class PhoneContactsModel {
private boolean isChecked;
String contactName;
String contactNumber;
public PhoneContactsModel(String contactName, String contactNumber, boolean isChecked) {
this.contactName = contactName;
this.contactNumber = contactNumber;
this.isChecked = isChecked;
}
protected PhoneContactsModel(Parcel in) {
isChecked = in.readByte() != 0;
contactName = in.readString();
contactNumber = in.readString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PhoneContactsModel)) return false;
PhoneContactsModel class1 = (PhoneContactsModel) o;
return contactNumber.equals(class1.contactNumber);
}
@Override
public int hashCode() {
return contactNumber.hashCode();
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
}
我尝试了下面的树集
public ArrayList<PhoneContactsModel> removeDuplicates(ArrayList<PhoneContactsModel> list) {
Set set = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (((PhoneContactsModel) o1).getContactNumber().equalsIgnoreCase(((PhoneContactsModel) o2).getContactNumber())) {
return 0;
}
return 1;
}
});
set.addAll(list);
list.clear();
final ArrayList newList = new ArrayList(set);
return newList;
}
我想根据电话号码过滤该arrayList。 但即使我使用HashSet,我也没有任何变化。 请帮助我任何建议,如果我做错了什么。 任何建议都被接受了。
答案 0 :(得分:0)
试试这个,
- 实体班
醇>PhoneContactsModel
public class PhoneContactsModel implements Parcelable{
private boolean isChecked;
String contactName;
String contactNumber;
public PhoneContactsModel(String contactName, String contactNumber, boolean isChecked) {
this.contactName = contactName;
this.contactNumber = contactNumber;
this.isChecked = isChecked;
}
protected PhoneContactsModel(Parcel in) {
isChecked = in.readByte() != 0;
contactName = in.readString();
contactNumber = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isChecked ? 1 : 0));
dest.writeString(contactName);
dest.writeString(contactNumber);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<PhoneContactsModel> CREATOR = new Creator<PhoneContactsModel>() {
@Override
public PhoneContactsModel createFromParcel(Parcel in) {
return new PhoneContactsModel(in);
}
@Override
public PhoneContactsModel[] newArray(int size) {
return new PhoneContactsModel[size];
}
};
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PhoneContactsModel)) return false;
PhoneContactsModel class1 = (PhoneContactsModel) o;
return contactNumber.equals(class1.contactNumber);
}
@Override
public int hashCode() {
return contactNumber.hashCode();
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
}
- 删除重复项
醇>
// I have added some dummy data
ArrayList<PhoneContactsModel> phoneContactsModelArrayList = new ArrayList<>();
phoneContactsModelArrayList.add(new PhoneContactsModel("User","123456789",true));
phoneContactsModelArrayList.add(new PhoneContactsModel("User","123456789",true));
phoneContactsModelArrayList.add(new PhoneContactsModel("User","123456789",true));
phoneContactsModelArrayList.add(new PhoneContactsModel("User1","123456788",true));
phoneContactsModelArrayList.add(new PhoneContactsModel("User2","123456787",true));
phoneContactsModelArrayList.add(new PhoneContactsModel("User3","123456786",true));
//uniques list
List<PhoneContactsModel> result = new ArrayList<PhoneContactsModel>();
// to just remove duplicates
Set<String> numbers= new HashSet<String>();
for( PhoneContactsModel phoneContactsModel: phoneContactsModelArrayList ) {
if( numbers.add( phoneContactsModel.getContactNumber() )) {
result.add( phoneContactsModel);
}
}