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"));

    }

}

1
  • 1
    How about a Map<String, Person> where the String (the map key) is the clientId? Commented May 18, 2020 at 7:05

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

9 Comments

Can you show Implementation of equals() method inside Person class which checks clientId
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 - 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.
it stills allowing duplicates. I tried this data perArrayList.add(new Person("16", 214, "Jimmy")) clientId is same but name and age are different
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?
|
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>();

Comments

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"));

3 Comments

But How can I implement Set in this case. I could not do that.
added example please check
@DilliBabuKadati - This will fail to achieve the objective until Person has equals method to return true when clientId is the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.