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