2

I am trying to add new object of Person class into arraylist. But I should avoid adding duplicate data with only one attribute of Person class. To be more precise Every Person has unique clientId. I need to check same clientIds should not be added into arraylist. When I try to add new Person Info with same clientId arraylist should just skip it. It does not matter other attributes are same except clientId. I am trying to check without looping Arraylist. I need more better way. I tried to use Set, But Set checks whole object. Any help is appreciated.

public class Person {

    private String clientId;
    private Integer age;
    private String name;

    public Person() {
    }


    public Person(String clientId, Integer age, String name) {
        this.clientId = clientId;
        this.age = age;
        this.name = name;
    }



    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class ManagePeople {


    public static void main(String[] args) {

        ArrayList<Person> perArrayList = new ArrayList<Person>();

        Person person;
        perArrayList.add(new Person("12", 100, "Tom"));
        perArrayList.add(new Person("14", 124, "Harold"));
        perArrayList.add(new Person("13", 234, "Petty"));
        perArrayList.add(new Person("15", 244, "Pedro"));
        perArrayList.add(new Person("16", 125, "Harry"));

    }

}

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Abdusoli
  • 661
  • 1
  • 8
  • 24

3 Answers3

1

You need to follow the following steps:

  1. Override equals() method (you should also override hashCode() along with equals()) in Person to return true when the clientId is same.

    class Person {
        private String clientId;
        // Other attributes
        // Constructor(s), getters, setters and other methods
    
        @Override
        public int hashCode() {
            return Objects.hash(clientId);
        }
    
        @Override
        public boolean equals(Object obj) {
            Person other = (Person) obj;
            return this.clientId.equals(other.clientId);
        }
    }
    
  2. Use a Set instead of ArrayList. The set will automatically discard the duplicate record (having same clientId as one of the existing records in the set) at the time of adding the record to it.

    Set<Person> personSet = new HashSet<Person>();
    personSet.add(new Person("12", 100, "Tom"));
    personSet.add(new Person("14", 124, "Harold"));
    personSet.add(new Person("13", 234, "Petty"));
    personSet.add(new Person("15", 244, "Pedro"));
    personSet.add(new Person("16", 125, "Harry"));
    personSet.add(new Person("16", 130, "Henry"));
    

    The record new Person("16", 130, "Henry") will automatically be discarded because it has the same clientId as one of the existing records. You can verify it by printing personSet.

Further reading: Why do I need to override the equals and hashCode methods in Java?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Can you show Implementation of equals() method inside Person class which checks clientId – Abdusoli May 18 '20 at 07:10
  • I have added equals method in my Person class you posted. But anyway it is allowing duplicates. Should I check equals result when adding data? – Abdusoli May 18 '20 at 07:18
  • @Abdusoli - When you will try to add a record with existing `clientId`, it will be automatically rejected. You do not have to implement anything for this. – Arvind Kumar Avinash May 18 '20 at 07:22
  • it stills allowing duplicates. I tried this data ```perArrayList.add(new Person("16", 214, "Jimmy")) ``` clientId is same but name and age are different – Abdusoli May 18 '20 at 07:31
  • No, it will discard `new Person("16", 214, "Jimmy")` automatically. Have you copied and pasted the code of my `hashCode()` and `equals()` methods as it is? Have you used `Set` instead of `ArrayList` as I've used? – Arvind Kumar Avinash May 18 '20 at 07:35
  • Updated my code above but it is allowing to add clientId 16 two times – Abdusoli May 18 '20 at 07:52
  • Should I change anything? – Abdusoli May 18 '20 at 08:05
  • @Abdusoli - I've corrected the problem. I'd left `name` and `age` inside my `hashCode()` method by mistake. Feel free to comment in case of any further doubt/issue. – Arvind Kumar Avinash May 18 '20 at 08:16
  • Thanks now it is working fine. Now I need to learn how hashCode() and equals(Object obj) functions working together – Abdusoli May 18 '20 at 08:46
1

Use HashMap and clientId as a key and value as your Person object. Map will not allow to add duplicte key.

HashMap<String, Person> tne = new HashMap<String, Person>();
Richard
  • 90
  • 11
0

Use Set instead of ArrayList, since Set allows to store only unique objects you can avoid duplicates.

Set<Person> perArrayList = new HashSet<>();

Person person;
perArrayList.add(new Person("12", 100, "Tom"));
perArrayList.add(new Person("14", 124, "Harold"));
perArrayList.add(new Person("13", 234, "Petty"));
perArrayList.add(new Person("15", 244, "Pedro"));
perArrayList.add(new Person("16", 125, "Harry"));