删除我模型的 ArrayList 中的重复项

Sup*_*Man 1 collections android model class duplicates

如果我想说实话,我不知道 android 或 java 中的任何集合的工作,但是我的问题和我的目的,我想删除我的模型或我的类的 ArrayList 中的重复项,这是我的模型,如下所示:

public class PersonClass {

    private String ID;
    private String Name;
    private String Family;
    private String Nickname;
    private String Email;
    private String Mobile;
    private Date BirthDate;
    private boolean Admin;
    private boolean Gender;
    private String ThumbnailURL;

    public PersonClass() {
    }

    public PersonClass(String ID, String name, String family, String nickname, String email, String mobile, Date birthDate, boolean admin, boolean gender, String thumbnailURL) {
        this.ID = ID;
        Name = name;
        Family = family;
        Nickname = nickname;
        Email = email;
        Mobile = mobile;
        BirthDate = birthDate;
        Admin = admin;
        Gender = gender;
        ThumbnailURL = thumbnailURL;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getFamily() {
        return Family;
    }

    public void setFamily(String family) {
        Family = family;
    }

    public String getNickname() {
        return Nickname;
    }

    public void setNickname(String nickname) {
        Nickname = nickname;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getMobile() {
        return Mobile;
    }

    public void setMobile(String mobile) {
        Mobile = mobile;
    }

    public Date getBirthDate() {
        return BirthDate;
    }

    public void setBirthDate(Date birthDate) {
        BirthDate = birthDate;
    }

    public boolean isAdmin() {
        return Admin;
    }

    public void setAdmin(boolean admin) {
        Admin = admin;
    }

    public boolean isGender() {
        return Gender;
    }

    public void setGender(boolean gender) {
        Gender = gender;
    }


    public String getThumbnailURL() {
        return ThumbnailURL;
    }

    public void setThumbnailURL(String thumbnailURL) {
        ThumbnailURL = thumbnailURL;
    }

    public static void LogConsole(PersonClass Person) {
        Log.i(MyApplication.LOG_TAG, "ID =" + Person.getID());
        Log.i(MyApplication.LOG_TAG, "Name :" + Person.getName());
        Log.i(MyApplication.LOG_TAG, "Family :" + Person.getFamily());
        Log.i(MyApplication.LOG_TAG, "Nickname :" + Person.getNickname());
        Log.i(MyApplication.LOG_TAG, "Email :" + Person.getEmail());
        Log.i(MyApplication.LOG_TAG, "Mobile :" + Person.getMobile());
        Log.i(MyApplication.LOG_TAG, "BirthDate :" + Person.getBirthDate());
        Log.i(MyApplication.LOG_TAG, "Admin :" + Person.isAdmin());
        Log.i(MyApplication.LOG_TAG, "Gender :" + Person.isGender());
        Log.i(MyApplication.LOG_TAG, "ThumbnailURL :" + Person.getThumbnailURL());
    }
}
Run Code Online (Sandbox Code Playgroud)

应该提到我可以通过 hashset 和 String Class 删除重复的 Array list of Array List of Person Class

AcceptList = WebService.MateList(AcceptRecordsJSONArray);
ArrayList<String> IDs1 = new ArrayList<>(AcceptList.size());
for (PersonClass person : AcceptList)
    IDs1.add(person.getID());
Log.e(MyApplication.LOG_TAG, "-------------------------------------IDs1(1)---------------------------------------");
for (String ID : IDs1)
    Log.i(MyApplication.LOG_TAG, "First time->ID=" + ID);
Log.e(MyApplication.LOG_TAG, "-------------------------------------IDs1(1)---------------------------------------");
Set<String> set = new LinkedHashSet<>(IDs1);
IDs1.clear();
IDs1.addAll(set);
Log.e(MyApplication.LOG_TAG, "-------------------------------------IDs1(2)---------------------------------------");
for (String ID : IDs1)
    Log.i(MyApplication.LOG_TAG, "Second time->ID=" + ID);
Log.e(MyApplication.LOG_TAG, "-------------------------------------IDs1(2)---------------------------------------");


Log.e(MyApplication.LOG_TAG, "-------------------------------------AcceptList(1)---------------------------------------");
for (PersonClass person : AcceptList)
    PersonClass.LogConsole(person);
Log.e(MyApplication.LOG_TAG, "-------------------------------------AcceptList(1)---------------------------------------");
ArrayList<PersonClass> AcceptList2 = removeDuplicates(AcceptList);
Log.e(MyApplication.LOG_TAG, "-------------------------------------AcceptList(2)---------------------------------------");
for (PersonClass person : AcceptList2)
    PersonClass.LogConsole(person);
Log.e(MyApplication.LOG_TAG, "-------------------------------------AcceptList(2)---------------------------------------");
Run Code Online (Sandbox Code Playgroud)

Hus*_*ain 8

ArrayList对象中删除重复项:

首先,我们需要重写equals()hashcode()你的方法PersonClass

@Override
public boolean equals(Object obj) {
    if (obj == this) return true;

    if (!(obj instanceof Person)) {
        return false;
    }

    Person person = (Person) obj;

    return person.id.equals(id);
} 

@Override
public int hashCode() {
    return id;
}
Run Code Online (Sandbox Code Playgroud)

根据您的类实现和使用,还有其他更好的方法可以做到这一点。在这里查看它们。

现在,删除重复的项目,使用HashSetLinkedHashSet

ArrayList<Person> duplicate = new ArrayList<Person>

duplicate.add(new Person())
... 
...
HashSet<Person> filter = new HashSet(duplicate);
ArrayList<Person> persons = new ArrayList<Person>(filter);
Run Code Online (Sandbox Code Playgroud)