1

If I want to honest you I don't know work by any collection in android or and java, but my question and my purpose, I want remove duplicate item in ArrayList of My Model or my class this is my Model in below:

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());
    }
}

and Should be mentioned I could by hashset and String Class remove duplicate 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)---------------------------------------");
Vadik Sirekanyan
  • 3,332
  • 1
  • 22
  • 29
Super Man
  • 49
  • 1
  • 9
  • Why don't use `Set` if you want only unique elements . Or if you want to remove the object with some duplicate property then just use a loop ? Which is your problem ? Pls specify – ADM Mar 09 '18 at 15:37
  • Sorry!!! I'm amateur in coding and I know answer my question is in the work with Comparator or hash code and or ... in the class but I don't know implement code and I can unability that.. – Super Man Mar 09 '18 at 15:54
  • Possible duplicate of [How to remove duplicate objects in a List without equals/hashcode?](https://stackoverflow.com/questions/6680157/how-to-remove-duplicate-objects-in-a-listmyobject-without-equals-hashcode) – ADM Mar 09 '18 at 15:55
  • thank you for link , but I was looking this link but... – Super Man Mar 09 '18 at 15:56
  • @ADM My Dear friend thank you very much, you're very lovely for me and humens – Super Man Mar 09 '18 at 19:44

1 Answers1

9

Removing duplicate items from ArrayList of objects:

First, we need to override equals() and hashcode() method of your 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;
}

There are other better ways to do it according to your class implementation and usage. Check them out here.

Now, removing the duplicate items, use HashSet or LinkedHashSet

ArrayList<Person> duplicate = new ArrayList<Person>

duplicate.add(new Person())
... 
...
HashSet<Person> filter = new HashSet(duplicate);
ArrayList<Person> persons = new ArrayList<Person>(filter);
Umair
  • 6,366
  • 15
  • 42
  • 50
Hussain
  • 1,243
  • 12
  • 21
  • thank you very much, you're very lovely for me and humens, I hope to arrive every thing in the your life!!! thanks!!! – Super Man Mar 09 '18 at 19:47